Add a password generator
authorDavid Kerkeslager <kerkeslager@gmail.com>
Tue, 22 Oct 2019 16:42:51 +0000 (12:42 -0400)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Tue, 22 Oct 2019 16:42:51 +0000 (12:42 -0400)
passgener.py [new file with mode: 0644]

diff --git a/passgener.py b/passgener.py
new file mode 100644 (file)
index 0000000..34bc17f
--- /dev/null
@@ -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)