annotate ppgen.py @ 9:35c468a37b54

Extend command line options to select 2nd language
author Bernhard Reiter <bernhard@intevation.de>
date Wed, 02 Oct 2019 15:04:28 +0200
parents 200c2c3c5f67
children 15d5b3961009
rev   line source
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
1 #!/usr/bin/env python3
4
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
2 """Create a random passphrase from a dictionary of words. BETA
0
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
4
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
8 specific randomness source which "should be unpredictable
0
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
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
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
3
757625ec8364 Comment added hint about SLT's Go implementation.
Bernhard Reiter <bernhard@intevation.de>
parents: 2
diff changeset
20 Related: There is a Go implementation started by Sascha L. Teichmann at
757625ec8364 Comment added hint about SLT's Go implementation.
Bernhard Reiter <bernhard@intevation.de>
parents: 2
diff changeset
21 https://bitbucket.org/s_l_teichmann/ppgen
757625ec8364 Comment added hint about SLT's Go implementation.
Bernhard Reiter <bernhard@intevation.de>
parents: 2
diff changeset
22
757625ec8364 Comment added hint about SLT's Go implementation.
Bernhard Reiter <bernhard@intevation.de>
parents: 2
diff changeset
23
8
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
24 Copyright 2016, 2017, 2018 by Intevation GmbH.
1
00ed7df30fe4 Checking for 8k entries now. Comment improvements.
Bernhard Reiter <bernhard@intevation.de>
parents: 0
diff changeset
25 Author: Bernhard E. Reiter <bernhard@intevation.de>
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
26
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
27 This file is Free Software under the Apache 2.0 license and thus
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
28 comes without any warranty (to extend permissible under applicable law).
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
29 """
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
30
4
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
31 import argparse
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
32 import math
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
33 import re
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
34 import sys
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
35
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
36 from random import SystemRandom
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
37 _srandom = SystemRandom()
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
38
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
39 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
40
4
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
41
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
42 def buildDictionary(options):
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
43 """Build up a dictionary of unique words, calculate stats."""
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
44 global tainted
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
45 d = []
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
46
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
47 # dictionary for testing
4
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
48 #d = ["abc", "aBc", "cde", "efg", "hij", "blubber",
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
49 # "jikf", "zug", "lmf", "opq"]
2
a099246680ae Fix for the unique test.
Bernhard Reiter <bernhard@intevation.de>
parents: 1
diff changeset
50 # 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
51 #d = [''.join('A' * 1000) for _ in range(1000)]
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
52
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
53 # Using the dictionary from Ding **customize**
9
35c468a37b54 Extend command line options to select 2nd language
Bernhard Reiter <bernhard@intevation.de>
parents: 8
diff changeset
54 d = readDingDict(options,
35c468a37b54 Extend command line options to select 2nd language
Bernhard Reiter <bernhard@intevation.de>
parents: 8
diff changeset
55 filename="/usr/share/trans/de-en",
35c468a37b54 Extend command line options to select 2nd language
Bernhard Reiter <bernhard@intevation.de>
parents: 8
diff changeset
56 useLeft=not options.second_language)
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
57
8
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
58 # for debugging purposes, dump dictionary
4
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
59 if options.ddump_filename:
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
60 print("Writing out dictionary in '{}'.".format(options.ddump_filename))
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
61 with open(options.ddump_filename, "w") as f:
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
62 for i in d:
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
63 f.write("{}\n".format(i))
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
64
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
65 # Print some stats on the dictionary to be used
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
66 dl = len(d)
8
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
67 if not options.just_passphrase:
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
68 print("Found {:d} dictionary entries.".format(dl))
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
69 print("|= Number of words |= possibilities |")
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
70 for i in range(1, 5):
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
71 print("| {:2d} | 2^{:4.1f} |".format(
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
72 i, math.log(dl**i, 2)))
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
73
1
00ed7df30fe4 Checking for 8k entries now. Comment improvements.
Bernhard Reiter <bernhard@intevation.de>
parents: 0
diff changeset
74 if dl < 8000:
8
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
75 sys.stderr.write("!Your dictionary is below 8k entries, "
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
76 "that is quite small!\n")
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
77 tainted = True
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
78 return d
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
79
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
80
8
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
81 def readDingDict(options, filename="/usr/share/trans/de-en", useLeft=False):
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
82 """Read dictionary with unique words from file in Ding format.
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
83
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
84 useLeft: Boolean to control which language to use
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
85
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
86 TODO: add option to use both languages for people that speak them both?
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
87 """
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
88
6
81f75c9aac84 Cleanup, minor: improves Comments. Bumps copyright.
Bernhard Reiter <bernhard@intevation.de>
parents: 5
diff changeset
89 dset = set() # using the datatype 'set' to avoid duplicates
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
90
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
91 splitter = re.compile(r"""\ \|\ # first pattern ' | '
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
92 |;\ # second pattern '; '
6
81f75c9aac84 Cleanup, minor: improves Comments. Bumps copyright.
Bernhard Reiter <bernhard@intevation.de>
parents: 5
diff changeset
93 |(?<=\S)/(?=\S) # 3.: '/' surrounded by chars
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
94 |\s+ # by whitespace
4
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
95 """, re.VERBOSE)
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
96
8
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
97 if not options.just_passphrase:
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
98 print("Reading entries from {}.".format(filename), end='')
4
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
99 counter = 0 # for progress or stopping early
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
100 with open(filename, "r") as f:
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
101 for line in f:
4
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
102 if line[0] == '#':
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
103 continue
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
104
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
105 # languages are separated by " :: "
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
106 p = line.partition(" :: ")
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
107 languageEntry = p[0] if useLeft else p[2]
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
108
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
109 for word in splitter.split(languageEntry):
7
8b2f8f439817 Improves: ding parser.
Bernhard Reiter <bernhard@intevation.de>
parents: 6
diff changeset
110 word = word.strip('(",.)\'!:;<>').rstrip('/')
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
111 if len(word) > 2 and not word[0] in '[{/':
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
112 dset.add(word)
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
113
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
114 #TODO: check for very common words and remove them?
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
115
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
116 counter += 1
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
117 ## stop early when debugging
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
118 #if counter > 10: break
8
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
119 if not options.just_passphrase and counter % 10000 == 0:
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
120 print('.', end='')
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
121 sys.stdout.flush()
8
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
122 if not options.just_passphrase:
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
123 print()
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
124
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
125 return list(dset)
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
126
4
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
127
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
128 def main():
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
129 global tainted
4
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
130
8
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
131 parser = argparse.ArgumentParser(
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
132 description=__doc__.splitlines()[0],
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
133 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
134 parser.add_argument('-n', '--number-of-words', type=int, default=4,
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
135 help='how many words to draw for the passphrase, '
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
136 'most useful with -j')
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
137 parser.add_argument('-j', '--just-passphrase', action="store_true",
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
138 help='only output the passphrase on a single line')
4
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
139 parser.add_argument('--ddump-filename',
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
140 help='filename to dump the dictionary to')
9
35c468a37b54 Extend command line options to select 2nd language
Bernhard Reiter <bernhard@intevation.de>
parents: 8
diff changeset
141 parser.add_argument('-2', '--second-language', action='store_true',
35c468a37b54 Extend command line options to select 2nd language
Bernhard Reiter <bernhard@intevation.de>
parents: 8
diff changeset
142 help='use the second language in the ding dictionary')
4
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
143 options = parser.parse_args()
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
144
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
145 dictionary = buildDictionary(options)
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
146
8
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
147 how_many = options.number_of_words
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
148
8
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
149 output_string = ""
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
150 if not options.just_passphrase:
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
151 print("\nGenerated passphrase with {}"
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
152 " randomly selected words:\n".format(how_many))
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
153 print(" ", end='')
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
154 separator = '\n '
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
155 else:
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
156 separator = ' '
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
157
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
158 # use a dictionary `words` with lower cased words for a rudimentary check
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
159 words = {}
8
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
160 for x in range(how_many):
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
161 word = _srandom.choice(dictionary)
4
85c65a597420 Improves: command line options and code style.
Bernhard Reiter <bernhard@intevation.de>
parents: 3
diff changeset
162 words[word.lower()] = True
8
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
163 output_string += word + separator
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
164
8
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
165 print(output_string)
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
166
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
167 if len(words) < how_many:
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
168 sys.stderr.write("! You've drawn a word more than once, this means:\n"
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
169 "! Your random generation is weak"
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
170 " or you are being very lucky.\n")
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
171 tainted = True
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
172
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
173 if tainted:
8
200c2c3c5f67 Adds comfort.
Bernhard Reiter <bernhard@intevation.de>
parents: 7
diff changeset
174 sys.exit("!!! Don't use the resulting passphrase !!!")
0
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
175
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
176 if __name__ == "__main__":
7558ecd1cbf1 Initial version.
Bernhard Reiter <bernhard@intevation.de>
parents:
diff changeset
177 main()
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)