line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Password::zxcvbn::Combinatorics; |
2
|
7
|
|
|
7
|
|
121691
|
use strict; |
|
7
|
|
|
|
|
29
|
|
|
7
|
|
|
|
|
219
|
|
3
|
7
|
|
|
7
|
|
38
|
use warnings; |
|
7
|
|
|
|
|
19
|
|
|
7
|
|
|
|
|
213
|
|
4
|
7
|
|
|
7
|
|
39
|
use Exporter 'import'; |
|
7
|
|
|
|
|
27
|
|
|
7
|
|
|
|
|
3696
|
|
5
|
|
|
|
|
|
|
our @EXPORT_OK=qw(nCk factorial enumerate_substitution_maps); |
6
|
|
|
|
|
|
|
our $VERSION = '1.1.2'; # VERSION |
7
|
|
|
|
|
|
|
# ABSTRACT: some combinatorial functions |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub nCk { |
11
|
2575
|
|
|
2575
|
1
|
4857
|
my ($n, $k) = @_; |
12
|
|
|
|
|
|
|
# from http://blog.plover.com/math/choose.html |
13
|
|
|
|
|
|
|
|
14
|
2575
|
50
|
|
|
|
5183
|
return 0 if $k > $n; |
15
|
2575
|
100
|
|
|
|
9860
|
return 1 if $k == 0; |
16
|
|
|
|
|
|
|
|
17
|
1047
|
|
|
|
|
1804
|
my $ret = 1; |
18
|
1047
|
|
|
|
|
2173
|
for my $d (1..$k) { |
19
|
1509
|
|
|
|
|
2469
|
$ret *= $n; |
20
|
1509
|
|
|
|
|
2312
|
$ret /= $d; |
21
|
1509
|
|
|
|
|
2501
|
--$n; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
1047
|
|
|
|
|
3855
|
return $ret; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# given as array of simple str-str hashrefs, returns a list without |
28
|
|
|
|
|
|
|
# duplicates |
29
|
|
|
|
|
|
|
sub _dedupe { |
30
|
2414
|
|
|
2414
|
|
4757
|
my ($subs) = @_; |
31
|
|
|
|
|
|
|
my %keyed = map { |
32
|
3882
|
|
|
|
|
6365
|
my $this_sub=$_; |
33
|
|
|
|
|
|
|
# build a string representing the substitution, use it as a |
34
|
|
|
|
|
|
|
# hash key, so duplicates get eliminated |
35
|
|
|
|
|
|
|
join( |
36
|
|
|
|
|
|
|
'-', |
37
|
3882
|
|
|
|
|
5986
|
map { "${_},$this_sub->{$_}" } sort keys %{$this_sub}, |
|
9772
|
|
|
|
|
33530
|
|
|
3882
|
|
|
|
|
11119
|
|
38
|
|
|
|
|
|
|
) => $this_sub |
39
|
2414
|
|
|
|
|
3960
|
} @{$subs}; |
|
2414
|
|
|
|
|
4700
|
|
40
|
2414
|
|
|
|
|
9739
|
return [values %keyed]; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub _recursive_enumeration { |
44
|
3951
|
|
|
3951
|
|
9898
|
my ($table,$keys,$subs) = @_; |
45
|
3951
|
100
|
|
|
|
7079
|
return $subs unless @{$keys}; |
|
3951
|
|
|
|
|
17049
|
|
46
|
2414
|
|
|
|
|
4661
|
my ($first_key,@rest_keys) = @{$keys}; |
|
2414
|
|
|
|
|
6717
|
|
47
|
2414
|
|
|
|
|
4781
|
my @next_subs; |
48
|
2414
|
|
|
|
|
4325
|
for my $value (@{$table->{$first_key}}) { |
|
2414
|
|
|
|
|
6312
|
|
49
|
2547
|
|
|
|
|
4390
|
for my $sub (@{$subs}) { |
|
2547
|
|
|
|
|
5320
|
|
50
|
|
|
|
|
|
|
# if we already have a reverse mapping for this, keep it |
51
|
|
|
|
|
|
|
push @next_subs, $sub |
52
|
3322
|
100
|
|
|
|
8809
|
if exists $sub->{$value}; |
53
|
|
|
|
|
|
|
# and add this new one |
54
|
3322
|
|
|
|
|
5288
|
push @next_subs, { %{$sub}, $value => $first_key }; |
|
3322
|
|
|
|
|
12611
|
|
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
2414
|
|
|
|
|
6343
|
my $deduped_next_subs = _dedupe(\@next_subs); |
59
|
2414
|
|
|
|
|
10420
|
return _recursive_enumeration($table,\@rest_keys,\@next_subs); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub enumerate_substitution_maps { |
64
|
1537
|
|
|
1537
|
1
|
26754
|
my ($table) = @_; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
return _recursive_enumeration( |
67
|
|
|
|
|
|
|
$table, |
68
|
1537
|
|
|
|
|
3782
|
[keys %{$table}], |
|
1537
|
|
|
|
|
8469
|
|
69
|
|
|
|
|
|
|
[{}], # it needs an accumulator with an initial empty element |
70
|
|
|
|
|
|
|
); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub factorial { |
75
|
25131
|
|
|
25131
|
1
|
39071
|
my $ret=1; |
76
|
25131
|
|
|
|
|
70258
|
$ret*=$_ for 1..$_[0]; |
77
|
25131
|
|
|
|
|
51319
|
return $ret; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
1; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
__END__ |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=pod |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=encoding UTF-8 |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=for :stopwords combinatorial |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 NAME |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Data::Password::zxcvbn::Combinatorics - some combinatorial functions |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 VERSION |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
version 1.1.2 |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 DESCRIPTION |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
This module provides a few combinatorial functions that are used |
101
|
|
|
|
|
|
|
throughout the library. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 FUNCTIONS |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head2 C<nCk> |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
my $combinations = nCk($available,$taken); |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Returns the binomial coefficient: |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
/ $available \ |
112
|
|
|
|
|
|
|
| | |
113
|
|
|
|
|
|
|
\ $taken / |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head2 C<enumerate_substitution_maps> |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
my $enumeration = enumerate_substitution_maps(\%substitutions); |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Given a hashref of arrayrefs, interprets it as a map of |
120
|
|
|
|
|
|
|
substitutions. Returns an arrayref of hashrefs, containing all |
121
|
|
|
|
|
|
|
reverse-substitutions. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
For example, given: |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
{'a' => ['@', '4']} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
("'a' can be replaced with either '@' or '4'") |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
it returns: |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
[{'@' => 'a'}, {'4' => 'a'}] ], |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
("in one case, '@' could have been substituted for 'a'; in the other, |
134
|
|
|
|
|
|
|
'4' could have been substituted for 'a'") |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 C<factorial> |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
my $fact = factorial($number); |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Returns the factorial of the given number. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 AUTHOR |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Gianni Ceccarelli <gianni.ceccarelli@broadbean.com> |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
This software is copyright (c) 2022 by BroadBean UK, a CareerBuilder Company. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
151
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=cut |