Skip to content

Instantly share code, notes, and snippets.

@AdriDevelopsThings
Created January 10, 2021 20:54
Show Gist options
  • Save AdriDevelopsThings/84a21caa250d4df469b29d2b0c08e44d to your computer and use it in GitHub Desktop.
Save AdriDevelopsThings/84a21caa250d4df469b29d2b0c08e44d to your computer and use it in GitHub Desktop.
Get the optimal password length from charset.
from math import log
DEFAULT_CHARSET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!§$%&/()=?+*#-_.:,;<>"
Entropie = lambda p: - sum([i * log(i, 2) for i in p])
charset = input(f"Charset [{DEFAULT_CHARSET}]: ")
if charset in (None, "", " "):
charset = DEFAULT_CHARSET
charset_length = len(charset)
p = 1 / charset_length
e = Entropie([p for i in range(charset_length)])
print("Optimal password length:", int(100 / e) + 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment