lib/String/MkPasswd.pm

filter: Statementstmt 100% 120 / 120 Branchbran 86.2% 50 / 58 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 1 totaltotal 95.5% 194 / 203 CC 32 SCAR 29.9
CC
32
Cov
96%
mkpasswd
30.4
_insert
2.0
_psplit
2.0
CRAP 19.8
↑ Summary script/mkpasswd.pl »
1 package String::MkPasswd;
2  
3 5 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Defaults.
10 5 ▶use constant LENGTH => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use constant MINNUM => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use constant MINLOWER => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use constant MINUPPER => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use constant MINSPECIAL => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use constant DISTRIBUTE => "";
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use constant FATAL => "";
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # A few conveniences for dealing with homographs
19 5 ▶use constant ALLOWAMBIGUOUS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use constant NOAMBIGUOUS => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 our %IS_AMBIGUOUS = (
22 'o' => 1, # easily confused with zero, especially when capitalized
23 '0' => 1, # easily confused with capital O
24 '1' => 1, # easily confused for lower l or capital I
25 'i' => 1, # especially when capitalized, easily confused for 1, lower l, or pipe
26 'l' => 1, # easily confused for 1 or capital I
27 'v' => 1, # a pair of these looks like w
28 'w' => 1, # one of these looks like a pair of v's
29 'c' => 1, # can be confused for a paren
30 '|' => 1, # easily confused with 1, lower l, or capital I
31 '_' => 1, # easily confused with dash
32 '-' => 1, # easily confused with underscore
33 '.' => 1, # easily confused with comma
34 ',' => 1, # easily confused with period
35 ':' => 1, # easily confused with colon
36 ';' => 1, # easily confused with semicolon
37 ']' => 1, # easily confused with } and )
38 '[' => 1, # easily confused with { and (
39 '(' => 1, # easily confused with { and [
40 ')' => 1, # easily confused with } and ]
41 '{' => 1, # easily confused with ( and [
42 '}' => 1, # easily confused with ) and ]
43 );
44  
45 5 our %EXPORT_TAGS = (
46 all => [ qw(mkpasswd) ],
47 );
48 5 our @EXPORT_OK = @{ $EXPORT_TAGS{all} };
49 5 our $VERSION = "0.05";
50 5 our $FATAL = "";
51  
52 5 my %keys = (
53 ALLOWAMBIGUOUS() => {
54 dist => {
55 lkeys => [ qw(q w e r t a s d f g z x c v b) ],
56 rkeys => [ qw(y u i o p h j k l n m) ],
57 lnums => [ qw(1 2 3 4 5 6) ],
58 rnums => [ qw(7 8 9 0) ],
59 lspec => [ qw(! @ $ %), "#" ],
60 rspec => [
61 qw(^ & * ( ) - = _ + [ ] { } \ | ; : ' " < > . ? /), ","
62 ],
63 },
64 undist => {
65 lkeys => [
66 qw(a b c d e f g h i j k l m n o p q r s t u v w x y z)
67 ],
68 lkeys => [
69 qw(a b c d e f g h i j k l m n o p q r s t u v w x y z)
70 ],
71 rkeys => [
72 qw(a b c d e f g h i j k l m n o p q r s t u v w x y z)
73 ],
74 lnums => [ qw(0 1 2 3 4 5 6 7 8 9) ],
75 rnums => [ qw(0 1 2 3 4 5 6 7 8 9) ],
76 lspec => [
77 qw(! @ $ % ~ ^ & * ( ) - = _ + [ ] { } \ | ; : ' " < > . ? /),
78 "#", ","
79 ],
80 rspec => [
81 qw(! @ $ % ~ ^ & * ( ) - = _ + [ ] { } \ | ; : ' " < > . ? /),
82 "#", ","
83 ],
84 },
85 }
86 );
87  
88  
89 # Build unambiguous (NOAMBIGUOUS) keys entries from the ALLOWAMBIGUOUS set
90 5 foreach my $distribution ( keys %{ $keys{ ALLOWAMBIGUOUS() } } ) {
91 10 foreach my $block ( keys %{ $keys{ ALLOWAMBIGUOUS() }->{ $distribution } } ) {
92 $keys{ NOAMBIGUOUS() }->{ $distribution }->{ $block } = [
93 grep
94 1000 { ! $IS_AMBIGUOUS{ $_ } }
95 60 @{ $keys{ ALLOWAMBIGUOUS() }->{ $distribution }->{ $block } }
96 ];
97 }
98 }
99  
100 234 ▶sub mkpasswd {
Subroutine mkpasswd: called CC=30 SCAR=34.1
Pod undocumented
101 234 ▶ my $class = shift if UNIVERSAL::isa $_[0], __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 234 234
102 234 my %args = @_;
103  
104 # Configuration.
105 234 ▶ my $length = $args{"-length"} || LENGTH;
Condition100%2/2
$args{'-length'} || 9
l!l
24210
MC/DC100%1/1
$args{'-length'} || 9
$args{'-length'}
Truth table100%2/2
A
A= $args{'-length'}
Aresult
11
00
106 my $minnum = defined $args{"-minnum"}
107 234 ▶ ? $args{"-minnum"}
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'-minnum'} ? : 4 230 234
108 : MINNUM;
109 my $minlower = defined $args{"-minlower"}
110 234 ▶ ? $args{"-minlower"}
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'-minlower'} ? : 4 230 234
111 : MINLOWER;
112 my $minupper = defined $args{"-minupper"}
113 234 ▶ ? $args{"-minupper"}
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'-minupper'} ? : 4 230 234
114 : MINUPPER;
115 my $minspecial = defined $args{"-minspecial"}
116 234 ▶ ? $args{"-minspecial"}
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'-minspecial'} ? : 4 230 234
117 : MINSPECIAL;
118 my $distribute = defined $args{"-distribute"}
119 234 ▶ ? $args{"-distribute"}
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'-distribute'} ? : 0 234 234
120 : DISTRIBUTE;
121 my $ambiguousity = defined $args{"-noambiguous"}
122 234 ▶ ? $args{"-noambiguous"}
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'-noambiguous'} ? : 200 34 234
123 : ALLOWAMBIGUOUS;
124 my $fatal = defined $args{"-fatal"}
125 234 ▶ ? $args{"-fatal"}
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'-fatal'} ? : 2 232 234
126 : FATAL;
127  
128 234 ▶ if ( $minnum + $minlower + $minupper + $minspecial > $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minnum + $minlower + $minupper + $minspecial > $length) 8 226 234
129 8 ▶ if ( $fatal || $FATAL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fatal or $String::MkPasswd::FATAL) { } 2 6 8
Condition100%3/3
$fatal or $String::MkPasswd::FATAL
l!l&&r!l&&!r
116
MC/DC100%2/2
$fatal or $String::MkPasswd::FATAL
$fatal $String::MkPasswd::FATAL
Truth table100%3/3
A or B
A= $fatalB= $String::MkPasswd::FATAL
ABresult
1X1
011
000
130 2 croak "Impossible to generate $length-character password with "
131 . "$minnum numbers, $minlower lowercase letters, "
132 . "$minupper uppercase letters and $minspecial special "
133 . "characters";
134 } else {
135 6 return;
136 }
137 }
138  
139 # If there is any underspecification, use additional lowercase letters.
140 226 $minlower = $length - ($minnum + $minupper + $minspecial);
141  
142 # Choose left or right starting hand.
143 226 my $initially_left = my $isleft = int rand 2;
144  
145 # Select distribution of keys.
146 226 ▶ my $lkeys = $distribute ? $keys{$ambiguousity}{dist}{lkeys} : $keys{$ambiguousity}{undist}{lkeys};
Branch: 1 branch
BranchTrueFalseTotal
$distribute ? : 0 226 226
147 226 ▶ my $rkeys = $distribute ? $keys{$ambiguousity}{dist}{rkeys} : $keys{$ambiguousity}{undist}{rkeys};
Branch: 1 branch
BranchTrueFalseTotal
$distribute ? : 0 226 226
148 226 ▶ my $lnums = $distribute ? $keys{$ambiguousity}{dist}{lnums} : $keys{$ambiguousity}{undist}{lnums};
Branch: 1 branch
BranchTrueFalseTotal
$distribute ? : 0 226 226
149 226 ▶ my $rnums = $distribute ? $keys{$ambiguousity}{dist}{rnums} : $keys{$ambiguousity}{undist}{rnums};
Branch: 1 branch
BranchTrueFalseTotal
$distribute ? : 0 226 226
150 226 ▶ my $lspec = $distribute ? $keys{$ambiguousity}{dist}{lspec} : $keys{$ambiguousity}{undist}{lspec};
Branch: 1 branch
BranchTrueFalseTotal
$distribute ? : 0 226 226
151 226 ▶ my $rspec = $distribute ? $keys{$ambiguousity}{dist}{rspec} : $keys{$ambiguousity}{undist}{rspec};
Branch: 1 branch
BranchTrueFalseTotal
$distribute ? : 0 226 226
152  
153 # Generate password.
154  
155 226 my @lpass = (undef) x $length; # password chars typed by left hand
156 226 my @rpass = (undef) x $length; # password chars typed by right hand
157 226 my ($left, $right);
158  
159 226 ($left, $right) = &_psplit($minnum, \$isleft);
160 226 ▶ for ( my $i = 0; $i < $left; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $left) 226 226 452
161 226 &_insert(\@lpass, $lnums->[rand @$lnums]);
162 }
163 226 ▶ for ( my $i = 0; $i < $right; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $right) 225 226 451
164 225 &_insert(\@rpass, $rnums->[rand @$rnums]);
165 }
166  
167 226 ($left, $right) = &_psplit($minlower, \$isleft);
168 226 ▶ for ( my $i = 0; $i < $left; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $left) 493 226 719
169 493 &_insert(\@lpass, $lkeys->[rand @$lkeys]);
170 }
171 226 ▶ for ( my $i = 0; $i < $right; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $right) 483 226 709
172 483 &_insert(\@rpass, $rkeys->[rand @$rkeys]);
173 }
174  
175 226 ($left, $right) = &_psplit($minupper, \$isleft);
176 226 ▶ for ( my $i = 0; $i < $left; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $left) 225 226 451
177 225 &_insert(\@lpass, uc $lkeys->[rand @$lkeys]);
178 }
179 226 ▶ for ( my $i = 0; $i < $right; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $right) 225 226 451
180 225 &_insert(\@rpass, uc $rkeys->[rand @$rkeys]);
181 }
182  
183 226 ($left, $right) = &_psplit($minspecial, \$isleft);
184 226 ▶ for ( my $i = 0; $i < $left; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $left) 224 226 450
185 224 &_insert(\@lpass, $lspec->[rand @$lspec]);
186 }
187 226 ▶ for ( my $i = 0; $i < $right; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $right) 1 226 227
188 1 &_insert(\@rpass, $rspec->[rand @$rspec]);
189 }
190  
191 # Merge results together.
192 226 ▶ my $lpass = join "", map { defined $_ ? $_ : () } @lpass;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 1168 1072 2240
193 226 ▶ my $rpass = join "", map { defined $_ ? $_ : () } @rpass;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 934 1293 2227
194  
195 226 ▶ return $initially_left ? "$lpass$rpass" : "$rpass$lpass";
Branch: 1 branch
BranchTrueFalseTotal
$initially_left ? : 106 120 226
196 }
197  
198 # Insert $char into password at a random position, thereby spreading the
199 # different kinds of characters throughout the password.
200 2102 ▶sub _insert {
Subroutine _insert: called CC=2 SCAR=6.9
201 2102 my $pass = shift; # ref = ARRAY
202 2102 my $char = shift;
203  
204 2102 my $pos;
205 2102 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
defined $pass->[$pos] and do { do { $pos = int rand 1 + @$pass } } 491 2102 2593
206 2593 $pos = int rand(1 + @$pass);
207 } while ( defined $pass->[$pos] );
208  
209 2102 $pass->[$pos] = $char;
210 }
211  
212 # Given a size, distribute between left and right hands, taking into account
213 # where we left off.
214 904 ▶sub _psplit {
Subroutine _psplit: called CC=2 SCAR=6.9
215 904 my $max = shift;
216 904 my $isleft = shift; # ref = SCALAR
217  
218 904 my ($left, $right);
219  
220 904 ▶ if ( $$isleft ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$isleft) { } 773 131 904
221 773 $right = int($max / 2);
222 773 $left = $max - $right;
223 773 $$isleft = !($max % 2);
224 } else {
225 131 $left = int($max / 2);
226 131 $right = $max - $left;
227 131 $$isleft = !($max % 2);
228 }
229  
230 904 return ($left, $right);
231 }
232  
233 5 1;
234  
235 __END__
↑ Summary script/mkpasswd.pl »