forked from scottmsul/Password-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword.py
More file actions
executable file
·61 lines (50 loc) · 1.42 KB
/
password.py
File metadata and controls
executable file
·61 lines (50 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python
import argparse
import random
import math
# extract words from words.txt
# assumes each word is on its own line, no quotes/spaces/etc.
def words_from_file():
f = open('words.txt', 'r')
lines = f.readlines()
f.close()
# remove \n
words = [line[:-1] for line in lines]
return words
# extracts a random word from a list of words
def random_word(words):
# SystemRandom relies on /dev/urandom
r = random.SystemRandom()
return r.choice(words)
# extracts n random words from a list of words
def random_words(words, n):
chosen = []
for i in range(n):
new_word = random_word(words)
chosen.append(new_word)
return chosen
def entropy_per_word(words):
n = len(words)
return math.log(n, 2)
def args():
parser = argparse.ArgumentParser(description='Randomly generate a password from a list of words')
parser.add_argument('-n', '--num', help='Number of words to generate. Defaults to 5.', type=int, default=5)
args = parser.parse_args()
return args
def run(num):
words = words_from_file()
chosen = random_words(words, num)
entropy = entropy_per_word(words)
total_entropy = entropy*num
password = ""
print "chosen words:"
for word in chosen:
print " " + word
password = password+word
print "entropy per word: " + str(entropy)
print "total entropy: " + str(total_entropy)
print "password:\t%s" % password
if __name__ == '__main__':
args = args()
n = args.num
run(n)