Commit my random junk
[sandbox] / passgener.py
1 import math, os, sys
2
3 PASSWORD_MIN_BITS_ENTROPY = 256
4
5 def open_dict():
6     try:
7         return open('/usr/share/dict/words')
8     except FileNotFoundError:
9         return open(' /usr/dict/words')
10
11 with open_dict() as dict_file:
12     dict_words = dict_file.readlines()
13
14 dict_word_count = len(dict_words)
15
16 passphrase_word_count = math.ceil(math.log(2**PASSWORD_MIN_BITS_ENTROPY, dict_word_count))
17
18 bytes_entropy_per_word = math.ceil(math.log(dict_word_count, 2**8))
19
20 print(bytes_entropy_per_word)
21
22 def rand_word():
23     try:
24         return dict_words[int.from_bytes(os.urandom(bytes_entropy_per_word), byteorder='little')].strip()
25     except IndexError:
26         return rand_word()
27
28 passphrase = ' '.join(rand_word() for i in range(passphrase_word_count))
29
30 print(passphrase)