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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
package Data::Password::zxcvbn::Match;
use Moo::Role;
use Carp;
use List::AllUtils qw(max);
use overload
'<=>' => \&compare,
'cmp' => \&compare,
bool => sub { 1 },
;
our $VERSION = '1.1.3'; # VERSION
# ABSTRACT: role for match objects
has token => (is => 'ro', required => 1); # string
has [qw(i j)] => (is => 'ro', required => 1); # ints
sub compare {
my ($self, $other) = @_;
return $self->i <=> $other->i || $self->j <=> $other->j;
}
requires 'make';
has guesses => (is => 'lazy', builder => 'estimate_guesses');
requires 'estimate_guesses';
sub guesses_log10 {
return log(shift->guesses)/log(10);
}
my $MIN_SUBMATCH_GUESSES_SINGLE_CHAR = 10;
my $MIN_SUBMATCH_GUESSES_MULTI_CHAR = 50;
# this is here only because ::BruteForce needs it
sub _min_guesses {
my ($self) = @_;
return length($self->token) == 1
? $MIN_SUBMATCH_GUESSES_SINGLE_CHAR
: $MIN_SUBMATCH_GUESSES_MULTI_CHAR;
}
sub guesses_for_password {
my ($self, $password) = @_;
my $min_guesses = length($self->token) < length($password)
? $self->_min_guesses()
: 1;
my $guesses = $self->guesses();
return max($min_guesses,$guesses);
}
sub get_feedback {
my ($self, $is_sole_match) = @_;
return {
warning => $self->feedback_warning($is_sole_match),
suggestions => $self->feedback_suggestions($is_sole_match),
};
}
requires 'feedback_warning', 'feedback_suggestions';
sub fields_for_json { qw(token i j guesses guesses_log10) }
sub TO_JSON {
my ($self) = @_;
return {
class => ref($self),
map { $_ => $self->$_ } $self->fields_for_json,
};
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Data::Password::zxcvbn::Match - role for match objects
=head1 VERSION
version 1.1.3
=head1 SYNOPSIS
package My::Password::Match::Something;
use Moo;
with 'Data::Password::zxcvbn::Match';
has some_info => (is=>'ro');
sub make {
my ($class, $password) = @_;
return [ $class->new({
token => some_substring_of($password),
i => position_of_first_char($token,$password),
j => position_of_last_char($token,$password),
some_info => whatever_needed(),
}) ];
}
sub estimate_guesses {
my ($self) = @_;
return $self->some_complexity_estimate();
}
sub feedback_warning { 'this is a bad idea' }
sub feedback_suggestions { return [ 'do something else' ] }
1;
=head1 DESCRIPTION
zxcvbn estimates the strength of a password by guessing which way a
generic password cracker would produce it, and then guessing after how
many tries it would produce it.
This role provides the basic behaviour and interface for the classes
that implement that guessing.
=head1 ATTRIBUTES
=head2 C<token>
Required string: the portion of the password that this object
matches. For example, if your class represents "sequences of digits",
an instance L<made|/make> from the password C<abc1234def> would have
C<< token => '1234' >>.
=head2 C<i>, C<j>
Required integers: the indices of the first and last character of
L</token> in the password. For the example above, we would have C<< i
=> 3, j => 6 >>.
=head2 C<guesses>
The estimated number of attempts that a generic password cracker would
need to guess the particular L</token>. The value for this attribute
is generated on demand by calling L<< /C<estimate_guesses> >>.
=head1 REQUIRED METHODS
=head2 C<make>
sub make {
my ($class, $password) = @_;
return [ $class->new(\%something), ... ];
}
This factory method should return a I<sorted> arrayref of instances,
one for each substring of the C<$password> that could be generated /
guessed with the logic that your class represents.
=head2 C<estimate_guesses>
sub estimate_guesses {
my ($self) = @_;
return $self->some_complexity_estimate();
}
This method should return an integer, representing an estimate of the
number of attempts that a generic password cracker would need to guess
the particular L</token> I<within the logic that your class
represents>. For example, if your class represents "sequences of
digits", you could hypothesise that the cracker would go in order from
1, so you'd write:
sub estimate_guesses { return 0 + shift->token }
=head2 C<feedback_warning>
This method should return a string (possibly empty), or an arrayref
C<[$string,@values]> suitable for localisation. The returned value
should explain what's wrong, e.g. 'this is a top-10 common password'.
=head2 C<feedback_suggestions>
This method should return a possibly-empty array of suggestions to
help choose a less guessable password. e.g. 'Add another word or two';
again, elements can be strings or arrayrefs for localisation.
=head1 METHODS
=head2 C<compare>
$match1 <=> $match2
$match1 cmp $match2
The comparison operators are overloaded to sort by L<< /C<i> >> and
L<< /C<j> >>, so a sorted list of matches will cover the password from
left to right.
=head2 C<guesses_log10>
The logarithm in base 10 of L<< /C<guesses> >>.
=head2 C<guesses_for_password>
my $guesses = $match->guesses_for_password($password);
This method will return the same value as L<< /C<guesses> >>, or some
minimum number of guesses, whichever is higher.
This is to make sure that all match have a measurable impact on the
estimation of the total complexity.
=head2 C<get_feedback>
my %feedback = %{ $match->get_feedback($is_sole_match) };
Returns a hashref, with verbal feedback to help choose better
passwords. The hash contains:
=over 4
=item *
C<warning>
string (or arrayref for localisation), produced by calling L<<
/C<feedback_warning> >>
=item *
C<suggestions>
arrayref of strings (or arrayrefs for localisation), produced by
calling L<< /C<feedback_suggestions> >>.
=back
=head2 C<TO_JSON>
=head2 C<fields_for_json>
Matches can be serialised to JSON. The serialisation will be a
dictionary with all the fields returned by L<< /C<fields_for_json>
>>. By default, it will contain C<token i j guesses guesses_log10>.
=head1 AUTHOR
Gianni Ceccarelli <gianni.ceccarelli@broadbean.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2022 by BroadBean UK, a CareerBuilder Company.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|