Passphrase/MD5Crypt.pm

filter: Statementstmt 97.2% 70 / 72 Branchbran 63.8% 23 / 36 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 16 / 16 Podpod 100% 6 / 6 totaltotal 79.3% 119 / 150 CC 23 SCAR 23.6
CC
23
Cov
81%
new
21.3
as_crypt
3.5
from_crypt
3.2
CRAP 10.6
« Passphrase/EggdropBlowfish.pm ↑ Summary Passphrase/Clear.pm »
1 =head1 NAME
2  
3 Authen::Passphrase::MD5Crypt - passphrases using the MD5-based Unix
4 crypt()
5  
6 =head1 SYNOPSIS
7  
8 use Authen::Passphrase::MD5Crypt;
9  
10 $ppr = Authen::Passphrase::MD5Crypt->new(
11 salt => "Vd3f8aG6",
12 hash_base64 => "GcsdF4YCXb0PM2UmXjIoI1");
13  
14 $ppr = Authen::Passphrase::MD5Crypt->new(
15 salt_random => 1,
16 passphrase => "passphrase");
17  
18 $ppr = Authen::Passphrase::MD5Crypt->from_crypt(
19 '$1$Vd3f8aG6$GcsdF4YCXb0PM2UmXjIoI1');
20  
21 $ppr = Authen::Passphrase::MD5Crypt->from_rfc2307(
22 '{CRYPT}$1$Vd3f8aG6$GcsdF4YCXb0PM2UmXjIoI1');
23  
24 $salt = $ppr->salt;
25 $hash_base64 = $ppr->hash_base64;
26  
27 if($ppr->match($passphrase)) { ...
28  
29 $passwd = $ppr->as_crypt;
30 $userPassword = $ppr->as_rfc2307;
31  
32 =head1 DESCRIPTION
33  
34 An object of this class encapsulates a passphrase hashed using
35 the MD5-based Unix crypt() hash function. This is a subclass of
36 L<Authen::Passphrase>, and this document assumes that the reader is
37 familiar with the documentation for that class.
38  
39 The crypt() function in a modern Unix actually supports several
40 different passphrase schemes. This class is concerned only with one
41 particular scheme, an MD5-based algorithm designed by Poul-Henning Kamp
42 and originally implemented in FreeBSD. To handle the whole range of
43 passphrase schemes supported by the modern crypt(), see the
44 L<from_crypt|Authen::Passphrase/from_crypt> constructor and the
45 L<as_crypt|Authen::Passphrase/as_crypt> method in L<Authen::Passphrase>.
46  
47 The MD5-based crypt() scheme uses the whole passphrase, a salt which
48 can in principle be an arbitrary byte string, and the MD5 message
49 digest algorithm. First the passphrase and salt are hashed together,
50 yielding an MD5 message digest. Then a new digest is constructed,
51 hashing together the passphrase, the salt, and the first digest, all in
52 a rather complex form. Then this digest is passed through a thousand
53 iterations of a function which rehashes it together with the passphrase
54 and salt in a manner that varies between rounds. The output of the last
55 of these rounds is the resulting passphrase hash.
56  
57 In the crypt() function the raw hash output is then represented in ASCII
58 as a 22-character string using a base 64 encoding. The base 64 digits
59 are "B<.>", "B</>", "B<0>" to "B<9>", "B<A>" to "B<Z>", "B<a>" to "B<z>"
60 (in ASCII order). Because the base 64 encoding can represent 132 bits
61 in 22 digits, more than the 128 required, the last digit can only take
62 four of the base 64 digit values. An additional complication is that
63 the bytes of the raw algorithm output are permuted in a bizarre order
64 before being represented in base 64.
65  
66 There is no tradition of handling these passphrase hashes in raw
67 binary form. The textual encoding described above, including the final
68 permutation, is used universally, so this class does not support any
69 binary format.
70  
71 The complex algorithm was designed to be slow to compute, in order
72 to resist brute force attacks. However, the complexity is fixed,
73 and the operation of Moore's Law has rendered it far less expensive
74 than intended. If efficiency of a brute force attack is a concern,
75 see L<Authen::Passphrase::BlowfishCrypt>.
76  
77 =cut
78  
79 package Authen::Passphrase::MD5Crypt;
80  
81 2 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
82 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 2 ▶use Authen::Passphrase 0.003;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
87 2 ▶use Crypt::PasswdMD5 1.0 qw(unix_md5_crypt);
Subroutine BEGIN: called CC=1 SCAR=0.0
88 2 ▶use Crypt::SysRandom 'random_bytes';
Subroutine BEGIN: called CC=1 SCAR=0.0
89 2 ▶use MIME::Base64 'encode_base64';
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 2 our $VERSION = "0.009";
92  
93 2 ▶use parent "Authen::Passphrase";
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 =head1 CONSTRUCTORS
96  
97 =over
98  
99 =item Authen::Passphrase::MD5Crypt->new(ATTR => VALUE, ...)
100  
101 Generates a new passphrase recogniser object using the MD5-based crypt()
102 algorithm. The following attributes may be given:
103  
104 =over
105  
106 =item B<salt>
107  
108 The salt, as a raw string. It may be any byte string, but in crypt()
109 usage it is conventionally limited to zero to eight base 64 digits.
110  
111 =item B<salt_random>
112  
113 Causes salt to be generated randomly. The value given for this
114 attribute is ignored. The salt will be a string of eight base 64 digits.
115  
116 =item B<hash_base64>
117  
118 The hash, as a string of 22 base 64 digits. This is the final part of
119 what crypt() outputs.
120  
121 =item B<passphrase>
122  
123 A passphrase that will be accepted.
124  
125 =back
126  
127 The salt must be given, and either the hash or the passphrase.
128  
129 =cut
130  
131 9 ▶sub new {
Subroutine new: called CC=17 SCAR=30.6
Pod documented
132 9 my $class = shift;
133 9 my $self = bless({}, $class);
134 9 my $passphrase;
135 9 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 18 9 27
136 18 my $attr = shift;
137 18 my $value = shift;
138 18 ▶ if($attr eq "salt") {
Branch: 4 branches
BranchTrueFalseTotal
if ($attr eq 'salt') { } 8 10 18
elsif ($attr eq 'salt_random') { } 1 9 10
elsif ($attr eq 'hash_base64') { } 7 2 9
elsif ($attr eq 'passphrase') { } 2 0 2
139 croak "salt specified redundantly"
140 8 ▶ if exists $self->{salt};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"salt"} 0 8 8
141 8 ▶ $value =~ m#\A[\x00-\xff]*\z#
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A[\x00-\xff]*\z/ or croak("not a valid salt") 8 0 8
142 or croak "not a valid salt";
143 8 $self->{salt} = "$value";
144 } elsif($attr eq "salt_random") {
145 croak "salt specified redundantly"
146 1 ▶ if exists $self->{salt};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"salt"} 0 1 1
147 1 $self->{salt} = encode_base64(random_bytes(6), '');
148 1 $self->{salt} =~ tr{A-Za-z0-9+/=}{./0-9A-Za-z}d;
149 } elsif($attr eq "hash_base64") {
150 croak "hash specified redundantly"
151 7 ▶ if exists($self->{hash_base64}) ||
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"hash_base64"} or defined $passphrase 0 7 7
Condition33%1/3
exists $self->{"hash_base64"} or defined $passphrase
l!l&&r!l&&!r
007
MC/DC0%0/2
exists $self->{"hash_base64"} or defined $passphrase
exists $self->{"hash_base64"} defined $passphrase
Truth table33%1/3
A or B
A= exists $self->{"hash_base64"}B= defined $passphrase
ABresult
1X1
011
000
152 defined($passphrase);
153 7 ▶ $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 ] . 'MD5-based crypt() hash') 7 0 7
154 or croak "\"$value\" is not a valid ".
155 "MD5-based crypt() hash";
156 7 $self->{hash_base64} = "$value";
157 } elsif($attr eq "passphrase") {
158 croak "passphrase specified redundantly"
159 2 ▶ if exists($self->{hash_base64}) ||
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"hash_base64"} or defined $passphrase 0 2 2
Condition33%1/3
exists $self->{"hash_base64"} or defined $passphrase
l!l&&r!l&&!r
002
MC/DC0%0/2
exists $self->{"hash_base64"} or defined $passphrase
exists $self->{"hash_base64"} defined $passphrase
Truth table33%1/3
A or B
A= exists $self->{"hash_base64"}B= defined $passphrase
ABresult
1X1
011
000
160 defined($passphrase);
161 2 $passphrase = $value;
162 } else {
163 0 croak "unrecognised attribute `$attr'";
164 }
165 }
166 9 ▶ croak "salt not specified" unless exists $self->{salt};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"salt"} 0 9 9
167 9 ▶ $self->{hash_base64} = $self->_hash_base64_of($passphrase)
Branch: 1 branch
BranchTrueFalseTotal
if defined $passphrase 2 7 9
168 if defined $passphrase;
169 9 ▶ croak "hash not specified" unless exists $self->{hash_base64};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"hash_base64"} 0 9 9
170 9 return $self;
171 }
172  
173 =item Authen::Passphrase::MD5Crypt->from_crypt(PASSWD)
174  
175 Generates a new passphrase recogniser object using the MD5-based crypt()
176 algorithm, from a crypt string. The crypt string must consist of
177 "B<$1$>", the salt, "B<$>", then 22 base 64 digits giving the hash.
178 The salt may be up to 8 characters long, and cannot contain "B<$>"
179 or any character that cannot appear in a crypt string.
180  
181 =cut
182  
183 2 ▶sub from_crypt {
Subroutine from_crypt: called CC=3 SCAR=11.8
Pod documented
184 2 my($class, $passwd) = @_;
185 2 ▶ if($passwd =~ /\A\$1\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($passwd =~ /\A\$1\$/) 2 0 2
186 2 ▶ $passwd =~ m:\A\$1\$([!-#%-9;-~]{0,8})\$([./0-9A-Za-z]{22})\z:
Branch: 1 branch
BranchTrueFalseTotal
$passwd =~ m[\A\$1\$([!-#%-9;-~]{0,8})\$([./0-9A-Za-z]{22})\z] or croak("malformed \$1\$ data") 2 0 2
187 or croak "malformed \$1\$ data";
188 2 my($salt, $hash) = ($1, $2);
189 2 return $class->new(salt => $salt, hash_base64 => $hash);
190 }
191 0 return $class->SUPER::from_crypt($passwd);
192 }
193  
194 =item Authen::Passphrase::MD5Crypt->from_rfc2307(USERPASSWORD)
195  
196 Generates a new passphrase recogniser object using the MD5-based
197 crypt() algorithm, from an RFC 2307 string. The string must consist of
198 "B<{CRYPT}>" (case insensitive) followed by an acceptable crypt string.
199  
200 =back
201  
202 =head1 METHODS
203  
204 =over
205  
206 =item $ppr->salt
207  
208 Returns the salt, in raw form.
209  
210 =cut
211  
212 9 ▶sub salt {
Subroutine salt: called CC=1 SCAR=0.0
Pod documented
213 9 my($self) = @_;
214 9 return $self->{salt};
215 }
216  
217 =item $ppr->hash_base64
218  
219 Returns the hash value, as a string of 22 base 64 digits.
220  
221 =cut
222  
223 9 ▶sub hash_base64 {
Subroutine hash_base64: called CC=1 SCAR=0.0
Pod documented
224 9 my($self) = @_;
225 9 return $self->{hash_base64};
226 }
227  
228 =item $ppr->match(PASSPHRASE)
229  
230 =item $ppr->as_crypt
231  
232 =item $ppr->as_rfc2307
233  
234 These methods are part of the standard L<Authen::Passphrase> interface.
235 Not every passphrase recogniser of this type can be represented as a
236 crypt string: the crypt format only allows the salt to be up to eight
237 bytes, and it cannot contain any NUL or "B<$>" characters.
238  
239 =cut
240  
241 28 ▶sub _hash_base64_of {
Subroutine _hash_base64_of: called CC=3 SCAR=11.8
242 28 my($self, $passphrase) = @_;
243 die "can't use a crypt-incompatible salt yet ".
244 "(need generalised Crypt::MD5Passwd)"
245 if $self->{salt} =~ /[^\!-\#\%-9\;-\~]/ ||
246 28 ▶ length($self->{salt}) > 8;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"salt"} =~ /[^\!-\#\%-9\;-\~]/ or length $self->{"salt"} > 8 0 28 28
Condition33%1/3
$self->{"salt"} =~ /[^\!-\#\%-9\;-\~]/ or length $self->{"salt"} > 8
l!l&&r!l&&!r
0028
MC/DC0%0/2
$self->{"salt"} =~ /[^\!-\#\%-9\;-\~]/ or length $self->{"salt"} > 8
$self->{"salt"} =~ /[^\!-\#\%-9\;-\~]/ length $self->{"salt"} > 8
Truth table33%1/3
A or B
A= $self->{"salt"} =~ /[^\!-\#\%-9\;-\~]/B= length $self->{"salt"} > 8
ABresult
1X1
011
000
247 28 my $hash = unix_md5_crypt($passphrase, $self->{salt});
248 28 $hash =~ s/\A.*\$//;
249 28 return $hash;
250 }
251  
252 26 ▶sub match {
Subroutine match: called CC=1 SCAR=0.0
Pod documented
253 26 my($self, $passphrase) = @_;
254 26 return $self->_hash_base64_of($passphrase) eq $self->{hash_base64};
255 }
256  
257 10 ▶sub as_crypt {
Subroutine as_crypt: called CC=3 SCAR=12.5
Pod documented
258 10 my($self) = @_;
259 croak "can't put this salt into a crypt string"
260 if $self->{salt} =~ /[^\!-\#\%-9\;-\~]/ ||
261 10 ▶ length($self->{salt}) > 8;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"salt"} =~ /[^\!-\#\%-9\;-\~]/ or length $self->{"salt"} > 8 0 10 10
Condition33%1/3
$self->{"salt"} =~ /[^\!-\#\%-9\;-\~]/ or length $self->{"salt"} > 8
l!l&&r!l&&!r
0010
MC/DC0%0/2
$self->{"salt"} =~ /[^\!-\#\%-9\;-\~]/ or length $self->{"salt"} > 8
$self->{"salt"} =~ /[^\!-\#\%-9\;-\~]/ length $self->{"salt"} > 8
Truth table33%1/3
A or B
A= $self->{"salt"} =~ /[^\!-\#\%-9\;-\~]/B= length $self->{"salt"} > 8
ABresult
1X1
011
000
262 10 return "\$1\$".$self->{salt}."\$".$self->{hash_base64};
263 }
264  
265 =back
266  
267 =head1 SEE ALSO
268  
269 L<Authen::Passphrase>,
270 L<Crypt::PasswdMD5>
271  
272 =head1 AUTHOR
273  
274 Andrew Main (Zefram) <zefram@fysh.org>
275  
276 =head1 COPYRIGHT
277  
278 Copyright (C) 2006, 2007, 2009, 2010, 2012
279 Andrew Main (Zefram) <zefram@fysh.org>
280  
281 =head1 LICENSE
282  
283 This module is free software; you can redistribute it and/or modify it
284 under the same terms as Perl itself.
285  
286 =cut
287  
288 2 1;
« Passphrase/EggdropBlowfish.pm ↑ Summary Passphrase/Clear.pm »