annotate ppgen.py @ 0:7558ecd1cbf1

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