forked from OJezu/zxcvbn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.coffee
61 lines (49 loc) · 1.87 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
58
59
60
61
ranked_user_inputs_dict = {}
# initialize matcher lists
DICTIONARY_MATCHERS = [];
for dictionary_name of DICTIONARIES
DICTIONARY_MATCHERS.push build_dict_matcher(dictionary_name, build_ranked_dict(DICTIONARIES[dictionary_name]))
DICTIONARY_MATCHERS.push 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.
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].toLowerCase()] = i + 1
matches = omnimatch password
result = minimum_entropy_match_sequence password, matches
result.calc_time = time() - start
result
# make zxcvbn function globally available
# via window or exports object, depending on the environment
if window?
window.zxcvbn = zxcvbn
window.zxcvbn_load_hook?() # run load hook from user, if defined
else if exports?
exports.zxcvbn = zxcvbn