-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
executable file
·209 lines (179 loc) · 5.4 KB
/
Copy pathcode.py
File metadata and controls
executable file
·209 lines (179 loc) · 5.4 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python
import random
from flask import Flask, request, render_template
from math import log, floor
from decimal import Decimal
DEFAULT_COUNT = 4
GUESSES = 1000
SYMBOLS = 94
MIN_SECS = 60
HOUR_SECS = MIN_SECS * 60
DAY_SECS = HOUR_SECS * 24
YEAR_SECS = Decimal(DAY_SECS * 365.25)
WORDSFILE = '/usr/share/dict/words'
TEMPLATE_DIR = 'templates/'
EXPONENT_NAMES = (
'several',
'tens',
'hundreds',
'thousands',
'tens of thousands',
'hundreds of thousands',
'millions',
'tens of millions',
'hundreds of millions',
'billions',
'tens of billions',
'hundreds of billions',
'trillions',
'tens of trillions',
'hundreds of trillions',
'quadrillions',
'tens of quadrillions',
'hundreds of quadrillions',
'quintillions',
'tens of quintillions',
'hundreds of quintillions',
'sextillions',
'tens of sextillions',
'hundreds of sextillions',
'septillions',
'tens of septillions',
'hundreds of septillions',
'octillions',
'tens of octillions',
'hundreds of octillions',
'nonillions',
'tens of nonillions',
'hundreds of nonillions',
'decillions',
'tens of decillions',
'hundreds of decillions',
'undecillions',
'tens of undecillions',
'hundreds of undecillions',
'duodecillions',
'tens of duodecillions',
'hundreds of duodecillions',
'tredecillions',
'tens of tredecillions',
'hundreds of tredecillions',
'quattuorillions',
'tens of quattuorillions',
'hundreds of quattuorillions',
'quindecillions',
'tens of quindecillions',
'hundreds of quindecillions',
'sexdecillions',
'tens of sexdecillions',
'hundreds of sexdecillions',
'septendecillions',
'tens of septendecillions',
'hundreds of septendecillions',
'octodecillions',
'tens of octodecillions',
'hundreds of octodecillions',
'novemdecillions',
'tens of novemdecillions',
'hundreds of novemdecillions',
)
with open(WORDSFILE, 'r') as fp:
wordlist = [line.strip() for line in fp]
prefix = '/xkcd936'
app = Flask(__name__)
urls = (
'/', 'index',
prefix, 'index',
prefix + '/', 'index',
)
def describe_magnitude(exponent):
if exponent >= len(EXPONENT_NAMES):
return 'an absurdly large number'
else:
return EXPONENT_NAMES[exponent]
def describe_count(number, unit):
number = int(round(number))
if number > 1:
unit = unit + 's'
return '{} {}'.format(number, unit)
def describe_time(seconds):
seconds = Decimal(seconds)
if seconds >= YEAR_SECS:
years = seconds / YEAR_SECS
if years > 1000:
return '{} of years'.format(describe_magnitude(years.adjusted()))
else:
return describe_count(seconds / YEAR_SECS, 'year')
if seconds >= DAY_SECS:
return describe_count(seconds / DAY_SECS, 'day')
if seconds >= HOUR_SECS:
return describe_count(seconds / HOUR_SECS, 'hour')
if seconds >= MIN_SECS:
return describe_count(seconds / MIN_SECS, 'minute')
return describe_count(seconds, 'second')
def entropy_bits(total_entropy):
return int(floor(log(total_entropy, 2)))
@app.route('/')
def index():
words = wordlist
count = request.args.get('count', DEFAULT_COUNT)
propers = request.args.get('propers', 'false')
maxlength = request.args.get('maxlength', '')
try:
count = int(count)
except:
count = DEFAULT_COUNT
try:
maxlength = int(maxlength)
except:
maxlength = None
if propers != 'true':
words = [x for x in words if x[0].islower()]
if maxlength is not None and maxlength <= 0:
maxlength = None
if maxlength is not None and maxlength > 0:
words = [x for x in words if len(x) <= maxlength]
space = len(words)
possible = space ** count
base, exponent = '{:.2e}'.format(possible).split('e+', 1)
exponent = int(exponent)
magnitude = describe_magnitude(exponent)
sample = random.sample(words, count)
attacks = []
algentropy = possible
algguess = describe_time(algentropy / GUESSES)
attacks.append((
'Known algorithm',
space,
count,
entropy_bits(algentropy),
algguess,
'1',
'Assumes the attacker knows the exact algorithm used to '
'generate the password, including the wordlist and the number of '
'words used.',
))
symlen = sum([len(x) for x in sample]) + (len(sample) - 1)
symentropy = SYMBOLS ** symlen
symguess = describe_time(symentropy / GUESSES)
attacks.append((
'All symbols',
SYMBOLS,
symlen,
entropy_bits(symentropy),
symguess,
'2',
'Assumes the attacker knows nothing about the password, and is '
'trying all combinations of letters, numbers and symbols.'
))
return render_template('index.html',
words=sample,
space=space,
base=base,
exponent=exponent,
magnitude=magnitude,
count=count,
attacks=attacks,
propers=propers,
maxlength=maxlength,
)