From: David Kerkeslager Date: Tue, 22 Oct 2019 16:42:51 +0000 (-0400) Subject: Add a password generator X-Git-Url: https://code.kerkeslager.com/?p=sandbox;a=commitdiff_plain;h=545381e1397107d4ab00d2e54d85d3e11d98e4f1 Add a password generator --- diff --git a/passgener.py b/passgener.py new file mode 100644 index 0000000..34bc17f --- /dev/null +++ b/passgener.py @@ -0,0 +1,30 @@ +import math, os, sys + +PASSWORD_MIN_BITS_ENTROPY = 256 + +def open_dict(): + try: + return open('/usr/share/dict/words') + except FileNotFoundError: + return open(' /usr/dict/words') + +with open_dict() as dict_file: + dict_words = dict_file.readlines() + +dict_word_count = len(dict_words) + +passphrase_word_count = math.ceil(math.log(2**PASSWORD_MIN_BITS_ENTROPY, dict_word_count)) + +bytes_entropy_per_word = math.ceil(math.log(dict_word_count, 2**8)) + +print(bytes_entropy_per_word) + +def rand_word(): + try: + return dict_words[int.from_bytes(os.urandom(bytes_entropy_per_word), byteorder='little')].strip() + except IndexError: + return rand_word() + +passphrase = ' '.join(rand_word() for i in range(passphrase_word_count)) + +print(passphrase)