-
Notifications
You must be signed in to change notification settings - Fork 0
/
diceware.py
189 lines (154 loc) · 6.27 KB
/
diceware.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import argparse, os, re
from collections import namedtuple
##############################################################################
class Dice(object):
'''
Represents a dice that can be rolled to get a random value.
'''
def __init__(self, num_sides=6):
'''
Creates a new dice.
@param num_sides: The number of sides on the dice (2..20).
@param values: The values on each side of the dice.
'''
# Check and set the number of sides.
assert(num_sides >= 2 and num_sides <= 20)
self.num_sides = int(num_sides)
# There are 256 possible random values (0..255). In order to generate
# an even distribution of values, we need to range to be 0..N, where
# N+1 is cleanly divisible by num_sides. Any values greater than N
# will be ignored and a new random value generated.
# In order to avoid throwing too many values away, we'll choose the
# largest value of N that fits in the range.
divisor = int(256 / self.num_sides)
self.ceiling = (divisor * self.num_sides) - 1
def roll(self):
'''
Returns a random number, generated as if the user had rolled a dice
with a specified number of sides.
'''
# Find the next random value that's in the range 0..ceiling.
random = os.urandom(1)[0]
while random > self.ceiling:
random = os.urandom(1)[0]
# Find which side (0-based) was rolled, then add 1.
side = random % self.num_sides
return side + 1
##############################################################################
DicewarePassword = namedtuple('DicewarePassword', ['rolls', 'words'])
class DicewareWordListException(RuntimeError):
pass
class DicewareWordList(object):
DEFAULT_DICE_SIDES = 6
DEFAULT_NUM_DICE = 5
PARSE_OPTION = re.compile(r'^(D(?:ICE)|S(?:IDES))\s*[=:]\s*(\d+)$', re.I)
PARSE_WORD = re.compile(r'^(\d+)\s+(.*?)$')
dice_sides = DEFAULT_DICE_SIDES
num_dice = DEFAULT_NUM_DICE
dice = None
words = None
def load(self, wordlist):
# Prepare for the load.
self.words = {}
found_words = False
# Parse each line, looking for important values.
for line in wordlist:
line = line.strip()
# Skip empty lines
if not line:
continue
# Check if we've matched a word entry.
match = self.PARSE_WORD.match(line)
if match:
found_words = True
roll = match.group(1)
if self._is_valid_roll(roll):
self.words[roll] = match.group(2)
# If we haven't found any word entries yet, check for
if not found_words:
match = self.PARSE_OPTION.match(line)
if match:
opt = match.group(1).upper()
value = int(match.group(2))
if opt[0] == 'D':
self.num_dice = value
elif opt[0] == 'S':
self.dice_sides = value
# Generate a dice with the correct number of sides.
self.dice = Dice(self.dice_sides)
def _is_valid_roll(self, roll):
if len(roll) != self.num_dice:
return False
for dice_roll in list(roll):
if not int(dice_roll) in range(1, self.dice_sides + 1):
return False
return True
def verify(self):
if not self.words:
raise DicewareWordListException('No word list has been loaded.')
self._verify('')
def _verify(self, prefix):
for side in range(1, self.dice_sides + 1):
roll = prefix + str(side)
if len(roll) == self.num_dice:
if not self.words.get(roll, None):
raise DicewareWordListException('Word missing for dice roll "{0}".'.format(roll))
else:
self._verify(roll)
def get_password(self, num_words):
if not self.dice or not self.words:
return DicewarePassword([], [])
rolls = [self._roll_dice() for x in range(num_words)]
words = [self.words.get(roll) for roll in rolls]
return DicewarePassword(rolls, words)
def _roll_dice(self):
return ''.join(str(self.dice.roll()) for x in range(self.num_dice))
##############################################################################
parser = argparse.ArgumentParser('diceware.py',
description='Generates a diceware password.')
parser.add_argument('--wordlist', '-l',
type=argparse.FileType('r'),
default='wordlist.txt',
help='The path to a file containing a diceware word list.',
metavar='<file>')
parser.add_argument('--words', '-w',
type=int,
default=5,
help='The number of words in each password.',
metavar='<number>')
parser.add_argument('--passwords', '-n',
type=int,
default=1,
help='The number of passwords to generate.',
metavar='<number>')
parser.add_argument('--showrolls', '-r',
action='store_true',
default=False,
help='If present, the dice rolls will also be displayed.')
parser.add_argument('--out', '-o',
type=argparse.FileType('w'),
help='Write the password list to a file instead of stdout.',
metavar='<file>')
args = parser.parse_args()
##############################################################################
# Load and verify the word list.
wordlist = DicewareWordList()
try:
wordlist.load(args.wordlist)
wordlist.verify()
except DicewareWordListException as ex:
print(str(ex))
exit()
for x in range(args.passwords):
password = wordlist.get_password(args.words)
line = []
if args.showrolls:
line.append(' '.join(password.rolls))
line.append(' '.join(password.words))
if args.out:
args.out.write('\t'.join(line))
args.out.write('\n')
else:
print('\t'.join(line))
if args.out:
args.out.flush()