Passphrase/PHPass.pm

filter: Statementstmt 98.2% 111 / 113 Branchbran 68.3% 41 / 60 Conditioncond 52.3% 11 / 21 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 100% 20 / 20 Podpod 100% 9 / 9 totaltotal 83.0% 196 / 236 CC 38 SCAR 33.9
CC
38
Cov
84%
new
51.6
from_crypt
3.2
as_crypt
2.0
CRAP 29.6
« Passphrase/BlowfishCrypt.pm ↑ Summary Passphrase/Crypt16.pm »
1 =head1 NAME
2  
3 Authen::Passphrase::PHPass - passphrases using the phpass algorithm
4  
5 =head1 SYNOPSIS
6  
7 use Authen::Passphrase::PHPass;
8  
9 $ppr = Authen::Passphrase::PHPass->new(
10 cost => 10, salt => "NaClNaCl",
11 hash_base64 => "ObRxTm/.EiiYN02xUeAQs/");
12  
13 $ppr = Authen::Passphrase::PHPass->new(
14 cost => 10, salt_random => 1,
15 passphrase => "passphrase");
16  
17 $ppr = Authen::Passphrase::PHPass->from_crypt(
18 '$P$8NaClNaClObRxTm/.EiiYN02xUeAQs/');
19  
20 $ppr = Authen::Passphrase::PHPass->from_rfc2307(
21 '{CRYPT}$P$8NaClNaClObRxTm/.EiiYN02xUeAQs/');
22  
23 $cost = $ppr->cost;
24 $cost_base64 = $ppr->cost_base64;
25 $cost = $ppr->nrounds_log2;
26 $cost_base64 = $ppr->nrounds_log2_base64;
27 $salt = $ppr->salt;
28 $hash = $ppr->hash;
29 $hash_base64 = $ppr->hash_base64;
30  
31 if($ppr->match($passphrase)) { ...
32  
33 $passwd = $ppr->as_crypt;
34 $userPassword = $ppr->as_rfc2307;
35  
36 =head1 DESCRIPTION
37  
38 An object of this class encapsulates a passphrase hashed using
39 the phpass algorithm invented by Solar Designer and described
40 at L<http://www.openwall.com/phpass/>. This is a subclass of
41 L<Authen::Passphrase>, and this document assumes that the reader is
42 familiar with the documentation for that class.
43  
44 The phpass algorithm is based on the MD5 message digest algorithm.
45 There is an eight-byte salt, which is conventionally restricted to
46 consist of base 64 digits. There is also a cost parameter that controls
47 the expense of hashing. First the salt and passphrase are concatenated
48 and hashed by MD5. Then, 2^cost times, the hash from the previous stage
49 is concatenated with the passphrase and hashed by MD5. The passphrase
50 hash is the output from the final iteration.
51  
52 The passphrase hash is represented in ASCII using the crypt format with
53 prefix "B<$P$>". The first character after the format prefix is a base 64
54 digit giving the cost parameter. The next eight characters are the salt.
55 The salt is followed by 22 base 64 digits giving the hash. The base 64
56 digits are "B<.>", "B</>", "B<0>" to "B<9>", "B<A>" to "B<Z>", "B<a>"
57 to "B<z>" (in ASCII order).
58  
59 =cut
60  
61 package Authen::Passphrase::PHPass;
62  
63 2 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
64 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 2 ▶use Authen::Passphrase 0.003;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
69 2 ▶use Crypt::SysRandom 'random_bytes';
Subroutine BEGIN: called CC=1 SCAR=0.0
70 2 ▶use Digest::MD5 1.99_53 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 2 our $VERSION = "0.009";
73  
74 2 ▶use parent "Authen::Passphrase";
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 2 my $base64_digits = "./0123456789ABCDEFGHIJKLMNOPQRST".
77 "UVWXYZabcdefghijklmnopqrstuvwxyz";
78  
79 21 ▶sub _en_base64($) {
Subroutine _en_base64: called CC=2 SCAR=6.9
80 21 my($bytes) = @_;
81 21 my $nbytes = length($bytes);
82 21 my $npadbytes = 2 - ($nbytes + 2) % 3;
83 21 $bytes .= "\0" x $npadbytes;
84 21 my $digits = "";
85 21 ▶ for(my $i = 0; $i < $nbytes; $i += 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $nbytes) 122 21 143
86 122 my $v = ord(substr($bytes, $i, 1)) |
87 (ord(substr($bytes, $i+1, 1)) << 8) |
88 (ord(substr($bytes, $i+2, 1)) << 16);
89 122 $digits .= substr($base64_digits, $v & 0x3f, 1) .
90 substr($base64_digits, ($v >> 6) & 0x3f, 1) .
91 substr($base64_digits, ($v >> 12) & 0x3f, 1) .
92 substr($base64_digits, ($v >> 18) & 0x3f, 1);
93 }
94 21 substr $digits, -$npadbytes, $npadbytes, "";
95 21 return $digits;
96 }
97  
98 8 ▶sub _de_base64($) {
Subroutine _de_base64: called CC=2 SCAR=6.9
99 8 my($digits) = @_;
100 8 my $ndigits = length($digits);
101 8 my $npadbytes = 3 - ($ndigits + 3) % 4;
102 8 $digits .= "." x $npadbytes;
103 8 my $bytes = "";
104 8 ▶ for(my $i = 0; $i < $ndigits; $i += 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $ndigits) 48 8 56
105 48 my $v = index($base64_digits, substr($digits,$i,1)) |
106 (index($base64_digits, substr($digits,$i+1,1)) << 6) |
107 (index($base64_digits, substr($digits,$i+2,1)) << 12) |
108 (index($base64_digits, substr($digits,$i+3,1)) << 18);
109 48 $bytes .= chr($v & 0xff) .
110 chr(($v >> 8) & 0xff) .
111 chr(($v >> 16) & 0xff);
112 }
113 8 substr $bytes, -$npadbytes, $npadbytes, "";
114 8 return $bytes;
115 }
116  
117 =head1 CONSTRUCTORS
118  
119 =over
120  
121 =item Authen::Passphrase::PHPass->new(ATTR => VALUE, ...)
122  
123 Generates a new passphrase recogniser object using the phpass algorithm.
124 The following attributes may be given:
125  
126 =over
127  
128 =item B<cost>
129  
130 Base-two logarithm of the number of hashing rounds to perform.
131  
132 =item B<cost_base64>
133  
134 Base-two logarithm of the number of hashing rounds to perform, expressed
135 as a single base 64 digit.
136  
137 =item B<nrounds_log2>
138  
139 Synonym for B<cost>.
140  
141 =item B<nrounds_log2_base64>
142  
143 Synonym for B<cost_base64>.
144  
145 =item B<salt>
146  
147 The salt, as an eight-byte string.
148  
149 =item B<salt_random>
150  
151 Causes salt to be generated randomly. The value given for this
152 attribute is ignored. The salt will be a string of eight base 64 digits.
153  
154 =item B<hash>
155  
156 The hash, as a 16-byte string.
157  
158 =item B<hash_base64>
159  
160 The hash, as a string of 22 base 64 digits.
161  
162 =item B<passphrase>
163  
164 A passphrase that will be accepted.
165  
166 =back
167  
168 The cost and salt must be given, and either the hash or the passphrase.
169  
170 =cut
171  
172 11 ▶sub new {
Subroutine new: called CC=32 SCAR=39.4
Pod documented
173 11 my $class = shift;
174 11 my $self = bless({}, $class);
175 11 my $passphrase;
176 11 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 33 11 44
177 33 my $attr = shift;
178 33 my $value = shift;
179 33 ▶ if($attr eq "cost" || $attr eq "nrounds_log2") {
Branch: 7 branches
BranchTrueFalseTotal
if ($attr eq 'cost' or $attr eq 'nrounds_log2') { } 2 31 33
elsif ($attr eq 'cost_base64' or $attr eq 'nrounds_log2_base64') { } 9 22 31
elsif ($attr eq 'salt') { } 10 12 22
elsif ($attr eq 'salt_random') { } 1 11 12
elsif ($attr eq 'hash') { } 1 10 11
elsif ($attr eq 'hash_base64') { } 8 2 10
elsif ($attr eq 'passphrase') { } 2 0 2
Condition100%3/3
$attr eq "cost" or $attr eq "nrounds_log2"
l!l&&r!l&&!r
1131
MC/DC100%2/2
$attr eq "cost" or $attr eq "nrounds_log2"
$attr eq "cost" $attr eq "nrounds_log2"
Truth table100%3/3
A or B
A= $attr eq "cost"B= $attr eq "nrounds_log2"
ABresult
1X1
011
000
180 croak "cost specified redundantly"
181 2 ▶ if exists $self->{cost};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"cost"} 0 2 2
182 2 ▶ croak "\"$value\" is not a valid cost parameter"
Branch: 1 branch
BranchTrueFalseTotal
unless $value == int $value and $value >= 0 and $value <= 30 0 2 2
Condition33%2/6
$value == int $value and $value >= 0 and $value <= 30
!ll&&!rl&&r
002
$value == int $value and $value >= 0
!ll&&!rl&&r
002
MC/DC0%0/3
$value == int $value and $value >= 0 and $value <= 30
$value == int $value $value >= 0 $value <= 30
Truth table25%1/4
A and B and C
A= $value == int $valueB= $value >= 0C= $value <= 30
ABCresult
0XX0
10X0
1100
1111
183 unless $value == int($value) && $value >= 0 &&
184 $value <= 30;
185 2 $self->{cost} = 0+$value;
186 ▶ } elsif($attr eq "cost_base64" ||
Condition100%3/3
$attr eq "cost_base64" or $attr eq "nrounds_log2_base64"
l!l&&r!l&&!r
8122
MC/DC100%2/2
$attr eq "cost_base64" or $attr eq "nrounds_log2_base64"
$attr eq "cost_base64" $attr eq "nrounds_log2_base64"
Truth table100%3/3
A or B
A= $attr eq "cost_base64"B= $attr eq "nrounds_log2_base64"
ABresult
1X1
011
000
187 $attr eq "nrounds_log2_base64") {
188 croak "cost specified redundantly"
189 9 ▶ if exists $self->{cost};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"cost"} 0 9 9
190 9 ▶ croak "\"$value\" is not a valid cost parameter"
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ m[\A[./0-9A-S]\z] 0 9 9
191 unless $value =~ m#\A[./0-9A-S]\z#;
192 9 $self->{cost} = index($base64_digits, $value);
193 } elsif($attr eq "salt") {
194 croak "salt specified redundantly"
195 10 ▶ if exists $self->{salt};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"salt"} 0 10 10
196 10 ▶ $value =~ m#\A[\x00-\xff]{8}\z#
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A[\x00-\xff]{8}\z/ or croak(qq["$value" is not a valid salt]) 10 0 10
197 or croak "\"$value\" is not a valid salt";
198 10 $self->{salt} = "$value";
199 } elsif($attr eq "salt_random") {
200 croak "salt specified redundantly"
201 1 ▶ if exists $self->{salt};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"salt"} 0 1 1
202 1 $self->{salt} = _en_base64(random_bytes(6));
203 } elsif($attr eq "hash") {
204 croak "hash specified redundantly"
205 1 ▶ if exists($self->{hash}) ||
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"hash"} or defined $passphrase 0 1 1
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
l!l&&r!l&&!r
001
MC/DC0%0/2
exists $self->{"hash"} or defined $passphrase
exists $self->{"hash"} defined $passphrase
Truth table33%1/3
A or B
A= exists $self->{"hash"}B= defined $passphrase
ABresult
1X1
011
000
206 defined($passphrase);
207 1 ▶ $value =~ m#\A[\x00-\xff]{16}\z#
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A[\x00-\xff]{16}\z/ or croak("not a valid raw hash") 1 0 1
208 or croak "not a valid raw hash";
209 1 $self->{hash} = "$value";
210 } elsif($attr eq "hash_base64") {
211 croak "hash specified redundantly"
212 8 ▶ if exists($self->{hash}) ||
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"hash"} or defined $passphrase 0 8 8
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
l!l&&r!l&&!r
008
MC/DC0%0/2
exists $self->{"hash"} or defined $passphrase
exists $self->{"hash"} defined $passphrase
Truth table33%1/3
A or B
A= exists $self->{"hash"}B= defined $passphrase
ABresult
1X1
011
000
213 defined($passphrase);
214 8 ▶ $value =~ m#\A[./0-9A-Za-z]{21}[./01]\z#
Branch: 1 branch
BranchTrueFalseTotal
$value =~ m[\A[./0-9A-Za-z]{21}[./01]\z] or croak(qq["$value" is not a valid hash]) 8 0 8
215 or croak "\"$value\" is not a valid hash";
216 8 $self->{hash} = _de_base64($value);
217 } elsif($attr eq "passphrase") {
218 croak "passphrase specified redundantly"
219 2 ▶ if exists($self->{hash}) ||
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"hash"} or defined $passphrase 0 2 2
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
l!l&&r!l&&!r
002
MC/DC0%0/2
exists $self->{"hash"} or defined $passphrase
exists $self->{"hash"} defined $passphrase
Truth table33%1/3
A or B
A= exists $self->{"hash"}B= defined $passphrase
ABresult
1X1
011
000
220 defined($passphrase);
221 2 $passphrase = $value;
222 } else {
223 0 croak "unrecognised attribute `$attr'";
224 }
225 }
226 11 ▶ croak "cost not specified" unless exists $self->{cost};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"cost"} 0 11 11
227 11 ▶ croak "salt not specified" unless exists $self->{salt};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"salt"} 0 11 11
228 11 ▶ $self->{hash} = $self->_hash_of($passphrase) if defined $passphrase;
Branch: 1 branch
BranchTrueFalseTotal
if defined $passphrase 2 9 11
229 11 ▶ croak "hash not specified" unless exists $self->{hash};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"hash"} 0 11 11
230 11 return $self;
231 }
232  
233 =item Authen::Passphrase::PHPass->from_crypt(PASSWD)
234  
235 Generates a new phpass passphrase recogniser object from a crypt string.
236 The crypt string must consist of "B<$P$>", one base 64 character encoding
237 the cost, the salt, then 22 base 64 digits giving the hash. The salt
238 must be exactly 8 characters long, and cannot contain any character that
239 cannot appear in a crypt string.
240  
241 =cut
242  
243 2 ▶sub from_crypt {
Subroutine from_crypt: called CC=3 SCAR=11.8
Pod documented
244 2 my($class, $passwd) = @_;
245 2 ▶ if($passwd =~ /\A\$P\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($passwd =~ /\A\$P\$/) 2 0 2
246 2 ▶ $passwd =~ m#\A\$P\$([./0-9A-Za-z])([!-9;-~]{8})
Branch: 1 branch
BranchTrueFalseTotal
$passwd =~ m[\A\$P\$([./0-9A-Za-z])([!-9;-~]{8}) ([./0-9A-Za-z]{22})\z]x or croak("malformed \$P\$ data") 2 0 2
247 ([./0-9A-Za-z]{22})\z#x
248 or croak "malformed \$P\$ data";
249 2 my($cost, $salt, $hash) = ($1, $2, $3);
250 2 return $class->new(cost_base64 => $cost, salt => $salt,
251 hash_base64 => $hash);
252 }
253 0 return $class->SUPER::from_crypt($passwd);
254 }
255  
256 =item Authen::Passphrase::PHPass->from_rfc2307(USERPASSWORD)
257  
258 Generates a new phpass passphrase recogniser object from an RFC 2307
259 string. The string must consist of "B<{CRYPT}>" (case insensitive)
260 followed by an acceptable crypt string.
261  
262 =back
263  
264 =head1 METHODS
265  
266 =over
267  
268 =item $ppr->cost
269  
270 Returns the base-two logarithm of the number of hashing rounds that will
271 be performed.
272  
273 =cut
274  
275 10 ▶sub cost {
Subroutine cost: called CC=1 SCAR=0.0
Pod documented
276 10 my($self) = @_;
277 10 return $self->{cost};
278 }
279  
280 =item $ppr->cost_base64
281  
282 Returns the base-two logarithm of the number of hashing rounds that will
283 be performed, expressed as a single base 64 digit.
284  
285 =cut
286  
287 23 ▶sub cost_base64 {
Subroutine cost_base64: called CC=1 SCAR=0.0
Pod documented
288 23 my($self) = @_;
289 23 return substr($base64_digits, $self->{cost}, 1);
290 }
291  
292 =item $ppr->nrounds_log2
293  
294 Synonym for L</cost>.
295  
296 =cut
297  
298 2 *nrounds_log2 = \&cost;
299  
300 =item $ppr->nrounds_log2_base64
301  
302 Synonym for L</cost_base64>.
303  
304 =cut
305  
306 2 *nrounds_log2_base64 = \&cost_base64;
307  
308 =item $ppr->salt
309  
310 Returns the salt, as a string of eight bytes.
311  
312 =cut
313  
314 11 ▶sub salt {
Subroutine salt: called CC=1 SCAR=0.0
Pod documented
315 11 my($self) = @_;
316 11 return $self->{salt};
317 }
318  
319 =item $ppr->hash
320  
321 Returns the hash value, as a string of 16 bytes.
322  
323 =cut
324  
325 3 ▶sub hash {
Subroutine hash: called CC=1 SCAR=0.0
Pod documented
326 3 my($self) = @_;
327 3 return $self->{hash};
328 }
329  
330 =item $ppr->hash_base64
331  
332 Returns the hash value, as a string of 22 base 64 digits.
333  
334 =cut
335  
336 20 ▶sub hash_base64 {
Subroutine hash_base64: called CC=1 SCAR=0.0
Pod documented
337 20 my($self) = @_;
338 20 return _en_base64($self->{hash});
339 }
340  
341 =item $ppr->match(PASSPHRASE)
342  
343 =item $ppr->as_crypt
344  
345 =item $ppr->as_rfc2307
346  
347 These methods are part of the standard L<Authen::Passphrase> interface.
348  
349 =cut
350  
351 28 ▶sub _hash_of {
Subroutine _hash_of: called CC=2 SCAR=6.9
352 28 my($self, $passphrase) = @_;
353 28 my $ctx = Digest::MD5->new;
354 28 $ctx->add($self->{salt});
355 28 $ctx->add($passphrase);
356 28 my $hash = $ctx->digest;
357 28 ▶ for(my $i = 1 << $self->{cost}; $i--; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i--) 20128 28 20156
358 20128 $ctx = Digest::MD5->new;
359 20128 $ctx->add($hash);
360 20128 $ctx->add($passphrase);
361 20128 $hash = $ctx->digest;
362 }
363 28 return $hash;
364 }
365  
366 26 ▶sub match {
Subroutine match: called CC=1 SCAR=0.0
Pod documented
367 26 my($self, $passphrase) = @_;
368 26 return $self->_hash_of($passphrase) eq $self->{hash};
369 }
370  
371 10 ▶sub as_crypt {
Subroutine as_crypt: called CC=2 SCAR=7.1
Pod documented
372 10 my($self) = @_;
373 croak "can't put this salt into a crypt string"
374 10 ▶ if $self->{salt} =~ /[^!-9;-~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"salt"} =~ /[^!-9;-~]/ 0 10 10
375 10 return "\$P\$".$self->cost_base64.$self->{salt}.$self->hash_base64;
376 }
377  
378 =back
379  
380 =head1 SEE ALSO
381  
382 L<Authen::Passphrase>,
383 L<Digest::MD5>
384  
385 =head1 AUTHOR
386  
387 Andrew Main (Zefram) <zefram@fysh.org>
388  
389 =head1 COPYRIGHT
390  
391 Copyright (C) 2006, 2007, 2009, 2010, 2012
392 Andrew Main (Zefram) <zefram@fysh.org>
393  
394 =head1 LICENSE
395  
396 This module is free software; you can redistribute it and/or modify it
397 under the same terms as Perl itself.
398  
399 =cut
400  
401 2 1;
« Passphrase/BlowfishCrypt.pm ↑ Summary Passphrase/Crypt16.pm »