Last active
April 19, 2016 00:03
-
-
Save gr33n7007h/d2fc8f2f3ae1479cd1293c4c4cf5ba89 to your computer and use it in GitHub Desktop.
Calculate entropy of a password
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include Math | |
# returns the entropy in bits | |
def calculate_password_entropy(password) | |
return unless password.is_a? String | |
inc_digit = password[/\d/] ? 1 : 0 | |
inc_lower = password[/[a-z]/] ? 1 : 0 | |
inc_upper = password[/[A-Z]/] ? 1 : 0 | |
total = inc_digit * 10 + inc_upper * 26 + inc_lower * 26 | |
(log(total) * password.size / log(2.0)).floor | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment