forked from dropbox/zxcvbn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.coffee
57 lines (47 loc) · 1.85 KB
/
init.coffee
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
ranked_user_inputs_dict = {}
# initialize matcher lists
DICTIONARY_MATCHERS = [
build_dict_matcher('passwords', build_ranked_dict(passwords)),
build_dict_matcher('english', build_ranked_dict(english)),
build_dict_matcher('male_names', build_ranked_dict(male_names)),
build_dict_matcher('female_names', build_ranked_dict(female_names)),
build_dict_matcher('surnames', build_ranked_dict(surnames)),
build_dict_matcher('user_inputs', ranked_user_inputs_dict),
]
MATCHERS = DICTIONARY_MATCHERS.concat [
l33t_match,
digits_match, year_match, date_match,
repeat_match, sequence_match,
spatial_match
]
GRAPHS =
'qwerty': qwerty
'dvorak': dvorak
'keypad': keypad
'mac_keypad': mac_keypad
# on qwerty, 'g' has degree 6, being adjacent to 'ftyhbv'. '\' has degree 1.
# this calculates the average over all keys.
calc_average_degree = (graph) ->
average = 0
for key, neighbors of graph
average += (n for n in neighbors when n).length
average /= (k for k,v of graph).length
average
KEYBOARD_AVERAGE_DEGREE = calc_average_degree(qwerty)
KEYPAD_AVERAGE_DEGREE = calc_average_degree(keypad) # slightly different for keypad/mac keypad, but close enough
KEYBOARD_STARTING_POSITIONS = (k for k,v of qwerty).length
KEYPAD_STARTING_POSITIONS = (k for k,v of keypad).length
time = -> (new Date()).getTime()
# now that frequency lists are loaded, replace zxcvbn stub function.
window.zxcvbn = (password, user_inputs) ->
start = time()
if user_inputs?
for i in [0...user_inputs.length]
# update ranked_user_inputs_dict.
# i+1 instead of i b/c rank starts at 1.
ranked_user_inputs_dict[user_inputs[i]] = i + 1
matches = omnimatch password
result = minimum_entropy_match_sequence password, matches
result.calc_time = time() - start
result
window.zxcvbn_load_hook?() # run load hook from user, if defined