Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions data-scripts/build_keyboard_adjacency_graphs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/python
# coding: utf-8
import sys
import json as simplejson

Expand All @@ -10,29 +11,36 @@ def usage():
%s src/Matchers/adjacency_graphs.json
''' % sys.argv[0]

qwerty = r'''
qwerty = u'''
`~ 1! 2@ 3# 4$ 5% 6^ 7& 8* 9( 0) -_ =+
qQ wW eE rR tT yY uU iI oO pP [{ ]} \|
aA sS dD fF gG hH jJ kK lL ;: '"
zZ xX cC vV bB nN mM ,< .> /?
'''

dvorak = r'''
azerty = u'''
œŒ“ &1´ é2~ "3# '4{ (5[ -6| è7` _8\\ ç9^ à0@ )°] =+}
aAâ zZå eE€ rRç tTþ yYý uUû iIî oOô pP¶ ^"~ $£ê
qQÂ sSø dDÊ fF± gGæ hHð jJÛ kKÎ lLÔ mM¹ ù%² *µ³
<>| wW« xX» cC© vV® bBß nN¬ ,?¿ ;.× :/÷ !§¡
'''

dvorak = u'''
`~ 1! 2@ 3# 4$ 5% 6^ 7& 8* 9( 0) [{ ]}
'" ,< .> pP yY fF gG cC rR lL /? =+ \|
aA oO eE uU iI dD hH tT nN sS -_
;: qQ jJ kK xX bB mM wW vV zZ
'''

keypad = r'''
keypad = u'''
/ * -
7 8 9 +
4 5 6
1 2 3
0 .
'''

mac_keypad = r'''
mac_keypad = u'''
= / *
7 8 9 -
4 5 6 +
Expand Down Expand Up @@ -68,13 +76,21 @@ def build_graph(layout_str, slanted):
token_size = len(tokens[0])
x_unit = token_size + 1 # x position unit len is token len plus 1 for the following whitespace.
adjacency_func = get_slanted_adjacent_coords if slanted else get_aligned_adjacent_coords
assert all(len(token) == token_size for token in tokens), 'token len mismatch:\n ' + layout_str
for y, line in enumerate(layout_str.split('\n')):
for token in tokens:
assert len(token) == token_size, (
u'token "%s" len mismatch (%d != %d):\n%s ' % (
token, len(token), token_size, layout_str
).encode('utf-8')
)
for y, line in enumerate(layout_str.split(u'\n')):
# the way I illustrated keys above, each qwerty row is indented one space in from the last
slant = y - 1 if slanted else 0
for token in line.split():
x, remainder = divmod(line.index(token) - slant, x_unit)
assert remainder == 0, 'unexpected x offset for %s in:\n%s' % (token, layout_str)
assert remainder == 0, (
u'unexpected x offset for %s (%d != 0) in:\n%s' % (
token, remainder, layout_str)
).encode('utf8')
position_table[(x,y)] = token

adjacency_graph = {}
Expand All @@ -96,10 +112,11 @@ def build_graph(layout_str, slanted):
with open(sys.argv[1], 'w') as f:
data = {
'qwerty': build_graph(qwerty, True),
'azerty': build_graph(azerty, True),
'dvorak': build_graph(dvorak, True),
'keypad': build_graph(keypad, False),
'mac_keypad': build_graph(mac_keypad, False),
}
simplejson.dump(data, f)
f.write(simplejson.dumps(data, ensure_ascii=False).encode('utf8'))
sys.exit(0)

1 change: 1 addition & 0 deletions src/Matchers/SpatialMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ public static function getAdjacencyGraphs(): array
// can be found in multiple graphs (such as 789), the one that's listed first is that one that will be picked.
$data = [
'qwerty' => $data['qwerty'],
'azerty' => $data['azerty'],
'dvorak' => $data['dvorak'],
'keypad' => $data['keypad'],
'mac_keypad' => $data['mac_keypad'],
Expand Down
2 changes: 1 addition & 1 deletion src/Matchers/adjacency_graphs.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions test/Matchers/SpatialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ public function testShiftedCountForMultipleMatches(): void
"shifted count is correct for two matches in a row",
SpatialMatch::match($password),
'spatial',
['!QAZ', '1qaz'],
[[0, 3], [4, 7]],
['!QAZ', 'QAZ', '1qaz', 'qaz'],
[[0, 3], [1, 3], [4, 7], [5, 7]],
[
'graph' => ['qwerty', 'qwerty'],
'turns' => [1, 1],
'shiftedCount' => [4, 0],
'graph' => ['qwerty', 'azerty', 'qwerty', 'azerty'],
'turns' => [1, 2, 1, 2],
'shiftedCount' => [4, 2, 0, 0],
]
);
}
Expand Down
4 changes: 2 additions & 2 deletions test/ZxcvbnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ public function sanityCheckDataProvider()
return [
['password', 0, ['dictionary',], 'less than a second', 3],
['65432', 0, ['sequence',], 'less than a second', 101],
['sdfgsdfg', 1, ['repeat',], 'less than a second', 2595],
['sdfgsdfg', 0, ['repeat',], 'less than a second', 459.00000000299997],
['fortitude', 1, ['dictionary',], '1 second', 11308],
['dfjkym', 1, ['bruteforce',], '2 minutes', 1000001],
['fortitude22', 2, ['dictionary', 'repeat',], '2 minutes', 1140700],
['absoluteadnap', 2, ['dictionary', 'dictionary',], '25 minutes', 15187504],
['knifeandspoon', 3, ['dictionary', 'dictionary', 'dictionary'], '1 day', 1108057600],
['h1dden_26191', 3, ['dictionary', 'bruteforce', 'date'], '3 days', 2730628000],
['4rfv1236yhn!', 4, ['spatial', 'sequence', 'bruteforce'], '1 month', 38980000000],
['4rfv1236yhn!', 3, ['spatial', 'sequence', 'bruteforce'], '8 days', 6940000000.045],
['BVidSNqe3oXVyE1996', 4, ['bruteforce', 'regex',], 'centuries', 10000000000010000],
];
}
Expand Down