Skip to content

Instantly share code, notes, and snippets.

@gr33n7007h
Last active April 19, 2016 00:03
Show Gist options
  • Save gr33n7007h/d2fc8f2f3ae1479cd1293c4c4cf5ba89 to your computer and use it in GitHub Desktop.
Save gr33n7007h/d2fc8f2f3ae1479cd1293c4c4cf5ba89 to your computer and use it in GitHub Desktop.
Calculate entropy of a password
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