« Data/Password/zxcvbn/AuthorTools/BuildRankedDictionaries.pm
↑ Summary
Dist/Zilla/MintingProfile/zxcvbn.pm »
| 1 | package Data::Password::zxcvbn::AuthorTools::BuildAdjacencyGraphs; | ||
| 2 | use v5.26; | ||
| 3 | use Types::Common::String qw(NonEmptySimpleStr NonEmptyStr); | ||
| 4 | use Types::Standard qw(ArrayRef Dict Bool); | ||
| 5 | use List::Util qw(uniq); | ||
| 6 | use Moo; | ||
| 7 | use namespace::clean; | ||
| 8 | our $VERSION = '1.0.2'; # VERSION | ||
| 9 | # ABSTRACT: class to generate C<Data::Password::zxcvbn::*::AdjacencyGraph> | ||
| 10 | |||
| 11 | with 'Data::Password::zxcvbn::AuthorTools::PackageWriter'; | ||
| 12 | |||
| 13 | |||
| 14 | has layouts => ( | ||
| 15 | is => 'ro', | ||
| 16 | isa => ArrayRef[ | ||
| 17 | Dict[ | ||
| 18 | name => NonEmptySimpleStr, | ||
| 19 | layout => NonEmptyStr, | ||
| 20 | slanted => Bool, | ||
| 21 | ], | ||
| 22 | ], | ||
| 23 | required => 1, | ||
| 24 | ); | ||
| 25 | |||
| 26 | |||
| 27 | has '+package_abstract' => ( | ||
| 28 | default => 'adjacency graphs for common keyboards', | ||
| 29 | ); | ||
| 30 | |||
| 31 | # returns the six adjacent coordinates on a standard keyboard, where | ||
| 32 | # each row is slanted to the right from the last. adjacencies are | ||
| 33 | # clockwise, starting with key to the left, then two keys above, then | ||
| 34 | # right key, then two keys below. (that is, only near-diagonal keys | ||
| 35 | # are adjacent, so g's coordinate is adjacent to those of t,y,b,v, but | ||
| 36 | # not those of r,u,n,c.) | ||
| 37 | sub _get_slanted_adjacent_coords { | ||
| 38 | my ($x, $y) = @_; | ||
| 39 | return ( | ||
| 40 | [$x-1,$y], [$x, $y-1], [$x+1, $y-1], | ||
| 41 | [$x+1, $y], [$x, $y+1], [$x-1, $y+1], | ||
| 42 | ); | ||
| 43 | } | ||
| 44 | |||
| 45 | # returns the nine clockwise adjacent coordinates on a keypad, where | ||
| 46 | # each row is vert aligned. | ||
| 47 | sub _get_aligned_adjacent_coords { | ||
| 48 | my ($x, $y) = @_; | ||
| 49 | return ( | ||
| 50 | [$x-1, $y], [$x-1, $y-1], [$x, $y-1], [$x+1, $y-1], | ||
| 51 | [$x+1, $y], [$x+1, $y+1], [$x, $y+1], [$x-1, $y+1], | ||
| 52 | ) | ||
| 53 | } | ||
| 54 | |||
| 55 | sub _average_degree { | ||
| 56 | my ($graph) = @_; | ||
| 57 | |||
| 58 | my $average = 0; | ||
| 59 | for my $neighbors (values %{$graph}) { | ||
| 60 | $average += grep {defined} @{$neighbors}; | ||
| 61 | } | ||
| 62 | $average /= scalar(keys %{$graph}); | ||
| 63 | return $average; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub _build_position_table { | ||
| 67 | my ($self,$layout_str,$slanted) = @_; | ||
| 68 | |||
| 69 | my %position_table; | ||
| 70 | my @tokens = grep {length($_)} split /\s+/, $layout_str; | ||
| 71 | my $token_length = length($tokens[0]); | ||
| 72 | |||
| 73 | grep { length($_) != $token_length } @tokens | ||
| 74 | and die "token length mismatch:\n$layout_str\n"; | ||
| 75 | |||
| 76 | my $x_unit = $token_length + 1; | ||
| 77 | my $y=0; | ||
| 78 | for my $line (split /\n/,$layout_str) { | ||
| 79 | my $slant = $slanted ? $y - 1 : 0; | ||
| 80 | for my $token (split /\s+/,$line) { | ||
| 81 | next unless length($token); | ||
| 82 | my $x = int((index($line,$token) - $slant) / $x_unit); | ||
| 83 | $position_table{$x}->{$y} = $token; | ||
| 84 | } | ||
| 85 | ++$y; | ||
| 86 | } | ||
| 87 | |||
| 88 | return \%position_table; | ||
| 89 | } | ||
| 90 | |||
| 91 | # builds an adjacency graph as a dictionary: {character: | ||
| 92 | # [adjacent_characters]}. adjacent characters occur in a clockwise | ||
| 93 | # order. | ||
| 94 | # | ||
| 95 | # for example: | ||
| 96 | # | ||
| 97 | # * on qwerty layout, 'g' maps to ['fF', 'tT', 'yY', 'hH', 'bB', 'vV'] | ||
| 98 | # * on keypad layout, '7' maps to [undef, undef, undef, '=', '8', '5', | ||
| 99 | # '4', undef] | ||
| 100 | sub _build_graph { | ||
| 101 | my ($self, $layout_str, $slanted) = @_; | ||
| 102 | |||
| 103 | my $position_table = $self->_build_position_table( | ||
| 104 | $layout_str, $slanted, | ||
| 105 | ); | ||
| 106 | |||
| 107 | my $adjacency_function = $slanted | ||
| 108 | ? \&_get_slanted_adjacent_coords | ||
| 109 | : \&_get_aligned_adjacent_coords; | ||
| 110 | |||
| 111 | my %adjacency_graph; | ||
| 112 | for my $x (sort keys %{$position_table}) { | ||
| 113 | my $column = $position_table->{$x}; | ||
| 114 | for my $y (sort keys %{$column}) { | ||
| 115 | my $chars = $column->{$y}; | ||
| 116 | |||
| 117 | # that `uniq` is needed to support keys without shifted | ||
| 118 | # variant; such keys must be written as `xx` otherwise | ||
| 119 | # _build_position_table will ignore them, but we should | ||
| 120 | # not consider them as producing 2 separate identical | ||
| 121 | # characters, otherwise we get the adjacent nodes twice | ||
| 122 | for my $char (uniq split //,$chars) { | ||
| 123 | for my $coords ($adjacency_function->($x,$y)) { | ||
| 124 | push @{$adjacency_graph{$char}}, | ||
| 125 | $position_table->{$coords->[0]}->{$coords->[1]}; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | return \%adjacency_graph; | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | sub generate { | ||
| 136 | my ($self) = @_; | ||
| 137 | |||
| 138 | my %all_graphs; | ||
| 139 | for my $layout (@{$self->layouts}) { | ||
| 140 | my $graph = $self->_build_graph($layout->{layout}, $layout->{slanted}); | ||
| 141 | |||
| 142 | $all_graphs{ $layout->{name} } = { | ||
| 143 | keys => $graph, | ||
| 144 | average_degree => _average_degree($graph), | ||
| 145 | starting_positions => scalar keys %{$graph}, | ||
| 146 | }; | ||
| 147 | } | ||
| 148 | |||
| 149 | $self->write_out(\%all_graphs); | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | sub hash_variable_name { 'graphs' } | ||
| 154 | |||
| 155 | 1; | ||
| 156 | |||
| 157 | __END__ |