blib/lib/Text/Password/Pronounceable.pm

filter: Statementstmt 100% 102 / 102 Branchbran 85.4% 41 / 48 Conditioncond 82.3% 14 / 17 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 92.6% 177 / 191 CC 32 SCAR 17.3
CC
32
Cov
94%
generate
8.1
_build_password
7.0
_check_lengths
6.6
CRAP 5.6
↑ Summary
1 package Text::Password::Pronounceable;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Crypt::URandom ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 our $VERSION = '0.31';
9  
10 # frequency of English digraphs (from D Edwards 1/27/66)
11 4 my $frequency = [
12 [
13 4, 20, 28, 52, 2, 11, 28, 4, 32, 4, 6, 62, 23, 167,
14 2, 14, 0, 83, 76, 127, 7, 25, 8, 1, 9, 1
15 ], # aa - az
16 [
17 13, 0, 0, 0, 55, 0, 0, 0, 8, 2, 0, 22, 0, 0,
18 11, 0, 0, 15, 4, 2, 13, 0, 0, 0, 15, 0
19 ], # ba - bz
20 [
21 32, 0, 7, 1, 69, 0, 0, 33, 17, 0, 10, 9, 1, 0,
22 50, 3, 0, 10, 0, 28, 11, 0, 0, 0, 3, 0
23 ], # ca - cz
24 [
25 40, 16, 9, 5, 65, 18, 3, 9, 56, 0, 1, 4, 15, 6,
26 16, 4, 0, 21, 18, 53, 19, 5, 15, 0, 3, 0
27 ], # da - dz
28 [
29 84, 20, 55, 125, 51, 40, 19, 16, 50, 1,
30 4, 55, 54, 146, 35, 37, 6, 191, 149, 65,
31 9, 26, 21, 12, 5, 0
32 ], # ea - ez
33 [
34 19, 3, 5, 1, 19, 21, 1, 3, 30, 2, 0, 11, 1, 0,
35 51, 0, 0, 26, 8, 47, 6, 3, 3, 0, 2, 0
36 ], # fa - fz
37 [
38 20, 4, 3, 2, 35, 1, 3, 15, 18, 0, 0, 5, 1, 4,
39 21, 1, 1, 20, 9, 21, 9, 0, 5, 0, 1, 0
40 ], # ga - gz
41 [
42 101, 1, 3, 0, 270, 5, 1, 6, 57, 0, 0, 0, 3, 2,
43 44, 1, 0, 3, 10, 18, 6, 0, 5, 0, 3, 0
44 ], # ha - hz
45 [
46 40, 7, 51, 23, 25, 9, 11, 3, 0, 0, 2, 38, 25, 202,
47 56, 12, 1, 46, 79, 117, 1, 22, 0, 4, 0, 3
48 ], # ia - iz
49 [
50 3, 0, 0, 0, 5, 0, 0, 0, 1, 0, 0, 0, 0, 0,
51 4, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0
52 ], # ja - jz
53 [
54 1, 0, 0, 0, 11, 0, 0, 0, 13, 0, 0, 0, 0, 2,
55 0, 0, 0, 0, 6, 2, 1, 0, 2, 0, 1, 0
56 ], # ka - kz
57 [
58 44, 2, 5, 12, 62, 7, 5, 2, 42, 1, 1, 53, 2, 2,
59 25, 1, 1, 2, 16, 23, 9, 0, 1, 0, 33, 0
60 ], # la - lz
61 [
62 52, 14, 1, 0, 64, 0, 0, 3, 37, 0, 0, 0, 7, 1,
63 17, 18, 1, 2, 12, 3, 8, 0, 1, 0, 2, 0
64 ], # ma - mz
65 [
66 42, 10, 47, 122, 63, 19, 106, 12, 30, 1,
67 6, 6, 9, 7, 54, 7, 1, 7, 44, 124,
68 6, 1, 15, 0, 12, 0
69 ], # na - nz
70 [
71 7, 12, 14, 17, 5, 95, 3, 5, 14, 0, 0, 19, 41, 134,
72 13, 23, 0, 91, 23, 42, 55, 16, 28, 0, 4, 1
73 ], # oa - oz
74 [
75 19, 1, 0, 0, 37, 0, 0, 4, 8, 0, 0, 15, 1, 0,
76 27, 9, 0, 33, 14, 7, 6, 0, 0, 0, 0, 0
77 ], # pa - pz
78 [
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80 0, 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0
81 ], # qa - qz
82 [
83 83, 8, 16, 23, 169, 4, 8, 8, 77, 1, 10, 5, 26, 16,
84 60, 4, 0, 24, 37, 55, 6, 11, 4, 0, 28, 0
85 ], # ra - rz
86 [
87 65, 9, 17, 9, 73, 13, 1, 47, 75, 3, 0, 7, 11, 12,
88 56, 17, 6, 9, 48, 116, 35, 1, 28, 0, 4, 0
89 ], # sa - sz
90 [
91 57, 22, 3, 1, 76, 5, 2, 330, 126, 1,
92 0, 14, 10, 6, 79, 7, 0, 49, 50, 56,
93 21, 2, 27, 0, 24, 0
94 ], # ta - tz
95 [
96 11, 5, 9, 6, 9, 1, 6, 0, 9, 0, 1, 19, 5, 31,
97 1, 15, 0, 47, 39, 31, 0, 3, 0, 0, 0, 0
98 ], # ua - uz
99 [
100 7, 0, 0, 0, 72, 0, 0, 0, 28, 0, 0, 0, 0, 0,
101 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0
102 ], # va - vz
103 [
104 36, 1, 1, 0, 38, 0, 0, 33, 36, 0, 0, 4, 1, 8,
105 15, 0, 0, 0, 4, 2, 0, 0, 1, 0, 0, 0
106 ], # wa - wz
107 [
108 1, 0, 2, 0, 0, 1, 0, 0, 3, 0, 0, 0, 0, 0,
109 1, 5, 0, 0, 0, 3, 0, 0, 1, 0, 0, 0
110 ], # xa - xz
111 [
112 14, 5, 4, 2, 7, 12, 12, 6, 10, 0, 0, 3, 7, 5,
113 17, 3, 0, 4, 16, 30, 0, 0, 5, 0, 0, 0
114 ], # ya - yz
115 [
116 1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0,
117 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
118 ]
119 ]; # za - zz
120  
121 # We need to know the totals for each row
122 my $row_sums = [
123 map {
124 4 my $sum = 0;
125 104 map { $sum += $_ } @$_;
126 104 $sum;
127 } @$frequency
128 ];
129  
130  
131 # Frequency with which a given letter starts a word.
132 4 my $start_freq = [
133 1299, 425, 725, 271, 375, 470, 93, 223, 1009, 24,
134 20, 355, 379, 319, 823, 618, 21, 317, 962, 1991,
135 271, 104, 516, 6, 16, 14
136 ];
137  
138 4 my $total_sum = 0;
139 4 $total_sum += $_ for @$start_freq;
140  
141 # Store why the last attempt to generate a password failed,
142 # for the generate method to report.
143 4 my $GENERATE_ERROR;
144  
145 # A draw is four bytes wide, so it has this many possible values.
146 4 my $DRAW_BYTES = 4;
147 4 my $DRAW_SPACE = 2 ** ( 8 * $DRAW_BYTES );
148  
149 1021 ▶sub _check_lengths {
Subroutine _check_lengths: called CC=6 SCAR=18.8
150 1021 my ($min, $max) = @_;
151  
152 1021 ▶ Carp::carp "min length should be defined" unless defined $min;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min 0 1021 1021
153 1021 ▶ Carp::carp "min length should be > 0" unless $min>0;
Branch: 1 branch
BranchTrueFalseTotal
unless $min > 0 0 1021 1021
154  
155 1021 ▶ Carp::carp "max length should be defined" unless defined $max;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 0 1021 1021
156 1021 ▶ Carp::carp "max length should be > 0" unless $max>0;
Branch: 1 branch
BranchTrueFalseTotal
unless $max > 0 0 1021 1021
157  
158 1021 ▶ Carp::carp "max length must be >= min length" unless $min<=$max;
Branch: 1 branch
BranchTrueFalseTotal
unless $min <= $max 500 521 1021
159 }
160  
161 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
162 3 my $class = shift;
163 3 my ($min, $max) = @_;
164 3 ▶ $max ||= $min;
Condition100%3/3
$max ||= $min
l!l&&r!l&&!r
111
MC/DC100%2/2
$max ||= $min
$max $min
Truth table100%3/3
A ||= B
A= $maxB= $min
ABresult
1X1
011
000
165  
166 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 1 3
167 2 _check_lengths($min, $max);
168 }
169  
170 3 return bless { min => $min, max => $max }, $class;
171 }
172  
173 1022 ▶sub generate {
Subroutine generate: called CC=8 SCAR=20.9
Pod documented
174 1022 my $self = shift;
175 1022 my ($min, $max) = @_;
176  
177 1022 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 1019 3 1022
elsif (ref $self) { } 3 0 3
178 1019 ▶ $max ||= $min;
Condition66%2/3
$max ||= $min
l!l&&r!l&&!r
101360
MC/DC0%0/2
$max ||= $min
$max $min
Truth table66%2/3
A ||= B
A= $maxB= $min
ABresult
1X1
011
000
179 1019 _check_lengths($min, $max);
180 } elsif (ref $self) { # if given no arguments,
181 # use the factory settings (if any)
182 3 $min = $self->{min};
183 3 $max = $self->{max};
184 }
185 1022 ▶ if ( !$min && !$max ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($min or $max) 1 1021 1022
Condition66%2/3
$min or $max
l!l&&r!l&&!r
102101
MC/DC50%1/2
$min or $max
$min $max
Truth table66%2/3
A or B
A= $minB= $max
ABresult
1X1
011
000
186 # what? no parameters?
187 1 return q[]; # no random password
188 }
189  
190 1021 undef $GENERATE_ERROR;
191 1021 my $password = $self->_build_password( $min, $max );
192  
193 1020 ▶ if ( !defined $password ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $password) 5 1015 1020
194 5 ▶ my $error = defined $GENERATE_ERROR ? $GENERATE_ERROR : 'unknown error';
Branch: 1 branch
BranchTrueFalseTotal
defined $GENERATE_ERROR ? : 5 0 5
195 5 Carp::carp "Unable to generate a password: $error";
196 5 return q[]; # never fall back on weaker randomness
197 }
198  
199 # Return the password
200 1015 return $password;
201 }
202  
203 # A private helper function for RandomPassword
204 # Takes a row summary and a frequency chart for the next character to be searched
205 8132 ▶sub _generate_nextchar {
Subroutine _generate_nextchar: called CC=3 SCAR=11.0
206 8132 my $self = shift;
207 8132 my ( $all, $freq ) = @_;
208 8132 my ( $pos, $i );
209  
210 8132 $pos = $self->_random_int($all);
211 8132 ▶ return undef if !defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 1 8131 8132
212  
213 8131 ▶ for ( $i = 0 ;
Branch: 1 branch
BranchTrueFalseTotal
if ($pos >= $freq->[$i]) 87771 8131 95902
214 $pos >= $freq->[$i] ;
215 $pos -= $freq->[$i], $i++ )
216 {
217 }
218  
219 8131 return ($i);
220 }
221  
222 # The password itself, or undef if a draw from the CSPRNG failed.
223 1021 ▶sub _build_password {
Subroutine _build_password: called CC=7 SCAR=19.5
224 1021 my $self = shift;
225 1021 my ($min, $max) = @_;
226  
227 # When munging characters, we need to know where to start counting letters from
228 1021 my $a = ord('a');
229  
230 # Lengths run from $min to $max inclusive. A $max below $min is an error
231 # that _check_lengths carps about but has never been fatal: the old
232 # int( rand( $max - $min ) ) drew downwards from $min there, so keep that
233 # rather than silently pinning every such password to $min.
234 1021 my $span = $max - $min;
235 1021 ▶ my $offset = $self->_random_int( $span >= 0 ? $span + 1 : -$span );
Branch: 1 branch
BranchTrueFalseTotal
$span >= 0 ? : 521 500 1021
236 1021 ▶ return undef if !defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 4 1017 1021
237 1017 ▶ my $length = $span >= 0 ? $min + $offset : $min - $offset;
Branch: 1 branch
BranchTrueFalseTotal
$span >= 0 ? : 517 500 1017
238  
239 1017 my $char = $self->_generate_nextchar( $total_sum, $start_freq );
240 1016 ▶ return undef if !defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 1 1015 1016
241  
242 1015 my @word = ( $char + $a );
243 1015 for ( 2 .. $length ) {
244 7116 $char =
245 $self->_generate_nextchar( $row_sums->[$char],
246 $frequency->[$char] );
247 7116 ▶ return undef if !defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 7116 7116
248 7116 push ( @word, $char + $a );
249 }
250  
251 1015 return pack( "C*", @word );
252 }
253  
254 # A uniformly distributed integer in [0, $limit), or undef if the CSPRNG cannot
255 # be read. Crypt::URandom deals only in bytes, so a draw of four of them is
256 # unpacked into a number in [0, 2**32) and reduced modulo $limit.
257 #
258 # Modulo on its own would favor the low end of the range, since $limit rarely
259 # divides the draw space evenly. Draws landing in that leftover partial block
260 # are discarded and redrawn, leaving a span that does divide evenly. The block
261 # is a thin slice of 2**32, so a redraw should be very infrequent.
262 9187 ▶sub _random_int {
Subroutine _random_int: called CC=6 SCAR=17.9
263 9187 my ($self, $limit) = @_;
264 9187 ▶ return 0 if !$limit || $limit <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if not $limit or $limit <= 1 9 9178 9187
Condition100%3/3
not $limit or $limit <= 1
l!l&&r!l&&!r
189178
MC/DC100%2/2
not $limit or $limit <= 1
not $limit $limit <= 1
Truth table100%3/3
not(A) or B
A= not $limitB= $limit <= 1
ABresult
1X1
011
000
265  
266 # Only reachable from an absurd max length. Guarded because a wider limit
267 # would leave $ceiling at zero, so no draw could ever be accepted and the
268 # loop below would spin forever.
269 9178 ▶ if ( $limit > $DRAW_SPACE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit > $DRAW_SPACE) 2 9176 9178
270 2 $GENERATE_ERROR =
271 "a range of $limit values is wider than a $DRAW_BYTES byte draw";
272 2 return undef;
273 }
274  
275 9176 my $ceiling = int( $DRAW_SPACE / $limit ) * $limit;
276  
277 9176 while (1) {
278 9180 my $draw = _random_bytes($DRAW_BYTES);
279 9180 ▶ return undef if !defined $draw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $draw 4 9176 9180
280  
281 9176 my $value = unpack 'N', $draw;
282 9176 ▶ return $value % $limit if $value < $ceiling;
Branch: 1 branch
BranchTrueFalseTotal
if $value < $ceiling 9172 4 9176
283 }
284 }
285  
286 # Bytes from the system CSPRNG, or undef if none are to be had. Crypt::URandom
287 # dies on error. Trap that here.
288 9180 ▶sub _random_bytes {
Subroutine _random_bytes: called CC=5 SCAR=16.1
289 9180 my ($count) = @_;
290  
291 # $@ is localized so that a draw leaves the caller's copy alone, and so
292 # that nothing unwinding out of the eval can clobber the reason on the way.
293 9180 my ( $bytes, $err );
294 {
295 9180 local $@;
296 9180 $bytes = eval { Crypt::URandom::urandom($count) };
297 9180 $err = $@;
298 }
299  
300 9180 ▶ return $bytes if defined $bytes && length $bytes == $count;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bytes and length $bytes == $count 9176 4 9180
Condition100%3/3
defined $bytes and length $bytes == $count
!ll&&!rl&&r
319176
MC/DC100%2/2
defined $bytes and length $bytes == $count
defined $bytes length $bytes == $count
Truth table100%3/3
A and B
A= defined $bytesB= length $bytes == $count
ABresult
0X0
100
111
301  
302 4 my $why;
303 4 ▶ if ( defined $bytes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bytes) { } 1 3 4
304 1 $why = sprintf 'asked for %d byte(s) but received %d',
305 $count, length $bytes;
306 }
307 else {
308 3 ▶ $why = $err || 'unknown error';
Condition50%1/2
$err || 'unknown error'
l!l
30
MC/DC0%0/1
$err || 'unknown error'
$err
Truth table50%1/2
A
A= $err
Aresult
11
00
309 3 $why =~ s/\s+\z//;
310 }
311  
312 4 $GENERATE_ERROR
313 = "no secure source of randomness is available: $why";
314  
315 4 return undef;
316 }
317  
318  
319 4 1;
320  
321 =head1 NAME
322  
323 Text::Password::Pronounceable - Generate pronounceable passwords
324  
325 =head1 SYNOPSIS
326  
327 # Generate a pronounceable password that is between 6 and 10 characters.
328 Text::Password::Pronounceable->generate(6, 10);
329  
330 # Ditto
331 my $pp = Text::Password::Pronounceable->new(6, 10);
332 $pp->generate;
333  
334 =head1 DESCRIPTION
335  
336 This module generates pronuceable passwords, based the the English
337 digraphs by D Edwards.
338  
339 =head2 METHODS
340  
341 =over
342  
343 =item B<new>
344  
345 $pp = Text::Password::Pronounceable->new($min, $max);
346 $pp = Text::Password::Pronounceable->new($len);
347  
348 Construct a password factory with length limits of $min and $max.
349 Or create a password factory with fixed length if only one argument
350 is provided.
351  
352 =item B<generate>
353  
354 $pp->generate;
355 $pp->generate($len);
356 $pp->generate($min, $max);
357  
358 Text::Password::Pronounceable->generate($len);
359 Text::Password::Pronounceable->generate($min, $max);
360  
361 Generate password. If used as an instance method, arguments override
362 the factory settings.
363  
364 Returns an empty string on failure, emitting an appropriate error message via
365 Carp::carp.
366  
367 =back
368  
369 =head1 HISTORY
370  
371 This code derived from mpw.pl, a bit of code with a sordid history.
372  
373 =over 4
374  
375 =item *
376  
377 CPAN module by Chia-liang Kao 9/11/2006.
378  
379 =item *
380  
381 Perl cleaned up a bit by Jesse Vincent 1/14/2001.
382  
383 =item *
384  
385 Converted to perl from C by Marc Horowitz, 1/20/2000.
386  
387 =item *
388  
389 Converted to C from Multics PL/I by Bill Sommerfeld, 4/21/86.
390  
391 =item *
392  
393 Original PL/I version provided by Jerry Saltzer.
394  
395 =back
396  
397 =head1 LICENSE
398  
399 Copyright 2006 by Best Practical Solutions, LLC.
400  
401 This program is free software; you can redistribute it and/or modify it
402 under the same terms as Perl itself.
403  
404 See <http://www.perl.com/perl/misc/Artistic.html>
405  
406 =cut
↑ Summary