-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodiceware.py
More file actions
84 lines (58 loc) · 2.06 KB
/
nodiceware.py
File metadata and controls
84 lines (58 loc) · 2.06 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
from flask import Flask, jsonify, request
from rolldice import rolldice
import math
import os.path
MIN_ENTROPY = 32
MAX_ENTROPY = 128
WORDLISTS_PATH = 'wordlists'
VALID_WORDLISTS = ['4k', '3k', '4d_3', '5d', '4d_2']
STRENGTH_KEYWORDS = ['base', 'strong', 'stronger', 'strongest']
app = Flask(__name__)
def get_strength(entropy):
q = 100 / (MAX_ENTROPY - MIN_ENTROPY)
return round(math.log((entropy - MIN_ENTROPY) * q, 100) * 100)
def get_wordlist_path(name, lang='fr'):
if name not in VALID_WORDLISTS:
raise Exception
wordlist = 'wordlist_{}_{}.txt'.format(lang, name)
return os.path.join(WORDLISTS_PATH, wordlist)
def get_nbwords(wordlist, strength):
# TODO: clean the mess
if wordlist in ['4k', '3k', '5d']:
start_at = 4
else:
start_at = 5
strength_dict = zip(STRENGTH_KEYWORDS, range(start_at, start_at+len(STRENGTH_KEYWORDS)))
return {key: value for (key, value) in strength_dict}[strength]
def json_pass(listname, strength, unique_prefix=None, lang='fr'):
nb_words = get_nbwords(listname, strength)
with open(get_wordlist_path(listname, lang)) as f:
words, entropy = rolldice(f, nb_words=nb_words)
ret = {
'words': words,
'entropy': entropy,
'strength': get_strength(entropy),
'unique_prefix': unique_prefix
}
return jsonify(ret)
@app.route('/')
def index():
return app.send_static_file('dist/html/index.html')
@app.route('/api/passphrase')
@app.route('/api/passphrase/<lang>')
@app.route('/api/passphrase/<lang>/<strength>')
def passphrase(lang='fr', strength=None):
if strength is None:
if lang in STRENGTH_KEYWORDS:
strength = lang
lang = 'fr'
else:
strength = 'base'
if request.args.get('zipable'):
listname = '4d_3' if lang == 'fr' else '4d_2'
return json_pass(listname, strength, unique_prefix=3, lang=lang)
if lang == 'fr':
listname = '4k' if strength == 'base' else '3k'
else:
listname = '5d'
return json_pass(listname, strength, lang=lang)