Mercurial > ppgen
annotate ppgen.py @ 2:a099246680ae
Fix for the unique test.
This was working in simple tests before.
Thanks for Sascha L. Teichmann for detailed reporting.
author | Bernhard Reiter <bernhard@intevation.de> |
---|---|
date | Mon, 02 May 2016 09:23:18 +0200 |
parents | 00ed7df30fe4 |
children | 757625ec8364 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python3 |
2 """Create a passphrase from a few random words. DRAFT | |
3 | |
4 Relies on the entropy of python's | |
5 random.SystemRandom class | |
6 which (according to the documentation) calls os.urandom() | |
7 which (according to the documentation) calls the operating system | |
8 specific randomness source which "should be unpredictable | |
9 enough for cryptographic applications" | |
10 | |
11 Requires: | |
12 * Python v>=3.2 | |
13 * a dictionary, Ding's trans-de-en by default. | |
14 E.g. on a Debian/Ubuntu system in package "trans-de-en". | |
15 or from http://ftp.tu-chemnitz.de/pub/Local/urz/ding/de-en/ | |
16 | |
1
00ed7df30fe4
Checking for 8k entries now. Comment improvements.
Bernhard Reiter <bernhard@intevation.de>
parents:
0
diff
changeset
|
17 Uses a hardcoded filepath and language. |
0 | 18 Search for **customize** below to change it. |
19 | |
20 Copyright 2016 by Intevation GmbH. | |
1
00ed7df30fe4
Checking for 8k entries now. Comment improvements.
Bernhard Reiter <bernhard@intevation.de>
parents:
0
diff
changeset
|
21 Author: Bernhard E. Reiter <bernhard@intevation.de> |
0 | 22 |
23 This file is Free Software under the Apache 2.0 license and thus | |
24 comes without any warranty (to extend permissible under applicable law). | |
25 """ | |
26 | |
27 import math | |
28 import re | |
29 import sys | |
30 | |
31 from random import SystemRandom | |
32 _srandom = SystemRandom() | |
33 | |
34 tainted = False # to be set if we find a hint that the passphrase may be weak | |
35 | |
36 def buildDictionary(): | |
37 """Build up a dictionary of unique words, calculate stats.""" | |
38 global tainted | |
39 d = [] | |
40 | |
41 # dictionary for testing | |
42 #d = ["abc", "aBc", "cde", "efg", "hij", "blubber", "jikf", "zug", "lmf", "opq"] | |
2
a099246680ae
Fix for the unique test.
Bernhard Reiter <bernhard@intevation.de>
parents:
1
diff
changeset
|
43 # second test dictionary to show that different string functions are used. |
a099246680ae
Fix for the unique test.
Bernhard Reiter <bernhard@intevation.de>
parents:
1
diff
changeset
|
44 #d = [''.join('A' * 1000) for _ in range(1000)] |
0 | 45 |
46 # Using the dictionary from Ding **customize** | |
47 d = readDingDict(filename="/usr/share/trans/de-en", useLeft=True) | |
48 | |
49 ## for debugging purpuses, dump dictionary | |
50 #dumpfilename = "ddump.txt" | |
51 #print("Writing out {}.".format(dumpfilename)) | |
52 #with open(dumpfilename, "w") as f: | |
53 # for i in d: | |
54 # f.write("{}\n".format(i)) | |
55 | |
56 # Print some stats on the dictionary to be used | |
57 dl = len(d) | |
1
00ed7df30fe4
Checking for 8k entries now. Comment improvements.
Bernhard Reiter <bernhard@intevation.de>
parents:
0
diff
changeset
|
58 print("Found {:d} dictionary entries.".format(dl)) |
00ed7df30fe4
Checking for 8k entries now. Comment improvements.
Bernhard Reiter <bernhard@intevation.de>
parents:
0
diff
changeset
|
59 if dl < 8000: |
00ed7df30fe4
Checking for 8k entries now. Comment improvements.
Bernhard Reiter <bernhard@intevation.de>
parents:
0
diff
changeset
|
60 print("!Your dictionary is below 8k entries, that is quite small!") |
0 | 61 tainted = True |
62 | |
63 print("|= Number of words |= possibilities |") | |
64 for i in range(1,5): | |
65 print("| {:2d} | 2^{:4.1f} |".format( | |
66 i, math.log(dl**i,2))) | |
67 return d | |
68 | |
69 | |
70 def readDingDict(filename = "/usr/share/trans/de-en", useLeft=False): | |
71 """Read dictionary with unique words from file in Ding format. | |
72 | |
73 useLeft: Boolean to control which language to use | |
74 | |
75 TODO: add option to use both languages for people that speak them both? | |
76 """ | |
77 | |
78 dset = set() #using the datatype 'set' to aviod duplicates | |
79 | |
80 splitter = re.compile(r"""\ \|\ # first pattern ' | ' | |
81 |;\ # second pattern '; ' | |
82 |(?<=\S)/(?=\S) # 3.: '\' surrounded by chars | |
83 |\s+ # by whitespace | |
84 """,re.VERBOSE) | |
85 | |
86 print("Reading entries from {}.".format(filename), end='') | |
87 counter = 0 # for progress or stopping early | |
88 with open(filename, "r") as f: | |
89 for line in f: | |
90 if line[0] == '#': continue | |
91 | |
92 # languages are separated by " :: " | |
93 p = line.partition(" :: ") | |
94 languageEntry = p[0] if useLeft else p[2] | |
95 | |
96 for word in splitter.split(languageEntry): | |
97 word = word.strip('(",.)\'!:;').rstrip('/') | |
98 if len(word) > 2 and not word[0] in '[{/': | |
99 dset.add(word) | |
100 | |
101 #TODO: check for very common words and remove them? | |
102 | |
103 counter += 1 | |
104 ## stop early when debugging | |
105 #if counter > 10: break | |
106 if not counter % 10000: | |
107 print('.', end='') | |
108 sys.stdout.flush() | |
109 print() | |
110 | |
111 return list(dset) | |
112 | |
113 def main(): | |
114 global tainted | |
115 dictionary = buildDictionary() | |
116 | |
117 howMany = 4 | |
118 | |
119 # use a dictionary with lower case words for a simple check if | |
120 # our random source is okay | |
121 print("\nGenerated passphrase with {} randomly selected words:\n".format( | |
122 howMany)) | |
123 print(" ", end='') | |
124 words = {} | |
125 for x in range(howMany): | |
126 word = _srandom.choice(dictionary) | |
2
a099246680ae
Fix for the unique test.
Bernhard Reiter <bernhard@intevation.de>
parents:
1
diff
changeset
|
127 words[word.lower()]= True |
0 | 128 print(word, end='\n ') |
129 print("\n") | |
130 | |
131 if len(words) < howMany: | |
132 print("! Your random generator is weak") | |
133 print("! or you are being very lucky.") | |
134 tainted = True | |
135 | |
136 if tainted: | |
137 print("!!! Don't use the resulting passphrase !!!") | |
138 | |
139 if __name__ == "__main__": | |
140 main() |