blib/lib/Crypt/PasswdMD5.pm

filter: Statementstmt 100% 83 / 83 Branchbran 96.1% 25 / 26 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 10 / 10 Podpod 75.0% 3 / 4 totaltotal 94.7% 126 / 133 CC 17 SCAR 19.4
CC
17
Cov
97%
unix_md5_crypt
12.0
random_md5_salt
5.1
to64
2.0
CRAP 6.9
↑ Summary
1 package Crypt::PasswdMD5;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Crypt::URandom qw(urandom);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our @EXPORT = qw/unix_md5_crypt apache_md5_crypt/;
13 3 our @EXPORT_OK = (@EXPORT, 'random_md5_salt');
14 3 our $VERSION = '1.46';
15  
16 # ------------------------------------------------
17  
18 3 my($itoa64) = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
19 3 our($Magic) = q/$1$/; # Magic strings. Need 'our' because of local just below.
20 3 my($max_salt_length) = 8;
21  
22 # ------------------------------------------------
23  
24 sub apache_md5_crypt
25 2 ▶{
Subroutine apache_md5_crypt: called CC=1 SCAR=0.0
Pod documented
26 # Change the Magic string to match the one used by Apache.
27  
28 2 local $Magic = q/$apr1$/;
29  
30 2 return unix_md5_crypt(@_);
31  
32 } # End of apache_md5_crypt.
33  
34 # ------------------------------------------------
35  
36 sub random_md5_salt
37 12 ▶{
Subroutine random_md5_salt: called CC=5 SCAR=16.3
Pod documented
38 12 ▶ my($len) = shift || $max_salt_length;
Condition66%2/3
shift() || $max_salt_length
l!l&&r!l&&!r
930
MC/DC0%0/2
shift() || $max_salt_length
shift() $max_salt_length
Truth table66%2/3
A || B
A= shift()B= $max_salt_length
ABresult
1X1
011
000
39 12 my($salt) = '';
40  
41 # Sanity check.
42  
43 12 ▶ $len = $max_salt_length unless ( ($len >= 1) and ($len <= $max_salt_length) );
Branch: 1 branch
BranchTrueFalseTotal
unless $len >= 1 and $len <= $max_salt_length 1 11 12
Condition66%2/3
$len >= 1 and $len <= $max_salt_length
!ll&&!rl&&r
0111
MC/DC50%1/2
$len >= 1 and $len <= $max_salt_length
$len >= 1 $len <= $max_salt_length
Truth table66%2/3
A and B
A= $len >= 1B= $len <= $max_salt_length
ABresult
0X0
100
111
44 12 $salt .= substr($itoa64,unpack("C",urandom(1))&0x3F,1) for (1..$len);
45  
46 12 return $salt;
47  
48 } # End of random_md5_salt.
49  
50 # ------------------------------------------------
51  
52 sub to64
53 60 ▶{
Subroutine to64: called CC=2 SCAR=6.9
Pod undocumented
54 60 my($v, $n) = @_;
55 60 my($ret) = '';
56  
57 60 ▶ while (--$n >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (--$n >= 0) 220 60 280
58 {
59 220 $ret .= substr($itoa64, $v & 0x3f, 1);
60  
61 220 $v >>= 6;
62 }
63  
64 60 return $ret;
65  
66 } # End of to64.
67  
68 # ------------------------------------------------
69  
70 sub unix_md5_crypt
71 10 ▶{
Subroutine unix_md5_crypt: called CC=12 SCAR=24.8
Pod documented
72 10 my($pw, $salt) = @_;
73 10 ▶ $pw = Encode::encode('utf8', $pw) if Encode::is_utf8($pw);
Branch: 1 branch
BranchTrueFalseTotal
if &Encode::is_utf8($pw) 1 9 10
74  
75 10 ▶ if (defined $salt)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $salt) { } 8 2 10
76 {
77 8 $salt =~ s/^\Q$Magic//; # Take care of the magic string if present.
78 8 $salt =~ s/^(.*)\$.*$/$1/; # Salt can have up to 8 chars...
79 8 $salt = substr($salt, 0, 8);
80 }
81 else
82 {
83 2 $salt = random_md5_salt(); # In case no salt was proffered.
84 }
85  
86 10 my($ctx) = Digest::MD5 -> new; # Here we start the calculation.
87  
88 10 $ctx -> add($pw); # Original password...
89 10 $ctx -> add($Magic); # ...our magic string...
90 10 $ctx -> add($salt); # ...the salt...
91  
92 10 my($final) = Digest::MD5 -> new;
93  
94 10 $final -> add($pw);
95 10 $final -> add($salt);
96 10 $final -> add($pw);
97  
98 10 $final = $final -> digest;
99  
100 10 ▶ for (my $pl = length($pw); $pl > 0; $pl -= 16)
Branch: 1 branch
BranchTrueFalseTotal
if ($pl > 0) 8 10 18
101 {
102 8 ▶ $ctx -> add(substr($final, 0, $pl > 16 ? 16 : $pl) );
Branch: 1 branch
BranchTrueFalseTotal
$pl > 16 ? : 0 8 8
103 }
104  
105 # Now the 'weird' xform.
106  
107 10 ▶ for (my $i = length($pw); $i; $i >>= 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($i) 26 10 36
108 {
109 26 ▶ if ($i & 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($i & 1) { } 16 10 26
110 {
111 16 $ctx -> add(pack('C', 0) );
112 }
113  
114 # This comes from the original version, where a
115 # memset() is done to $final before this loop.
116  
117 else
118 {
119 10 $ctx -> add(substr($pw, 0, 1) );
120 }
121 }
122  
123 10 $final = $ctx -> digest;
124  
125 # The following is supposed to make things run slower.
126 # In perl, perhaps it'll be *really* slow!
127  
128 10 ▶ for (my $i = 0; $i < 1000; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 1000) 10000 10 10010
129 {
130 10000 my($ctx1) = Digest::MD5 -> new;
131  
132 10000 ▶ if ($i & 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($i & 1) { } 5000 5000 10000
133 {
134 5000 $ctx1 -> add($pw);
135 }
136 else
137 {
138 5000 $ctx1 -> add(substr($final, 0, 16) );
139 }
140  
141 10000 ▶ if ($i % 3)
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 3) 6660 3340 10000
142 {
143 6660 $ctx1 -> add($salt);
144 }
145  
146 10000 ▶ if ($i % 7)
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 7) 8570 1430 10000
147 {
148 8570 $ctx1 -> add($pw);
149 }
150  
151 10000 ▶ if ($i & 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($i & 1) { } 5000 5000 10000
152 {
153 5000 $ctx1 -> add(substr($final, 0, 16) );
154 }
155 else
156 {
157 5000 $ctx1 -> add($pw);
158 }
159  
160 10000 $final = $ctx1 -> digest;
161 }
162  
163 # Final xform
164  
165 10 my($passwd);
166  
167 10 $passwd = '';
168 10 $passwd .= to64(int(unpack('C', (substr($final, 0, 1) ) ) << 16)
169 | int(unpack('C', (substr($final, 6, 1) ) ) << 8)
170 | int(unpack('C', (substr($final, 12, 1) ) ) ), 4);
171 10 $passwd .= to64(int(unpack('C', (substr($final, 1, 1) ) ) << 16)
172 | int(unpack('C', (substr($final, 7, 1) ) ) << 8)
173 | int(unpack('C', (substr($final, 13, 1) ) ) ), 4);
174 10 $passwd .= to64(int(unpack('C', (substr($final, 2, 1) ) ) << 16)
175 | int(unpack('C', (substr($final, 8, 1) ) ) << 8)
176 | int(unpack('C', (substr($final, 14, 1) ) ) ), 4);
177 10 $passwd .= to64(int(unpack('C', (substr($final, 3, 1) ) ) << 16)
178 | int(unpack('C', (substr($final, 9, 1) ) ) << 8)
179 | int(unpack('C', (substr($final, 15, 1) ) ) ), 4);
180 10 $passwd .= to64(int(unpack('C', (substr($final, 4, 1) ) ) << 16)
181 | int(unpack('C', (substr($final, 10, 1) ) ) << 8)
182 | int(unpack('C', (substr($final, 5, 1) ) ) ), 4);
183 10 $passwd .= to64(int(unpack('C', substr($final, 11, 1) ) ), 2);
184  
185 10 return $Magic . $salt . q/$/ . $passwd;
186  
187 } # End of unix_md5_crypt.
188  
189 # ------------------------------------------------
190  
191 3 1;
192  
193 =pod
194  
195 =encoding utf-8
196  
197 =head1 NAME
198  
199 Crypt::PasswdMD5 - Provide interoperable MD5-based crypt() functions
200  
201 =head1 SYNOPSIS
202  
203 use strict;
204 use warnings;
205  
206 use Crypt::PasswdMD5;
207  
208 my($password) = 'seekrit';
209 my($salt) = 'pepperoni';
210 my($unix_crypted) = unix_md5_crypt($password, $salt);
211 my($apache_crypted) = apache_md5_crypt($password, $salt);
212  
213 Or:
214  
215 use strict;
216 use warnings;
217  
218 use Crypt::PasswdMD5 'random_md5_salt';
219  
220 my($length) = 7;
221 my($salt_1) = random_md5_salt($length);
222 my($salt_2) = random_md5_salt(); # Default to $length == 8.
223  
224  
225 =head1 DESCRIPTION
226  
227 C<apache_md5_crypt()> provides a function compatible with Apache's C<.htpasswd> files.
228 This was contributed by Bryan Hart <bryan@eai.com>.
229 This function is exported by default.
230  
231 The C<unix_md5_crypt()> provides a crypt()-compatible interface to the rather new MD5-based crypt() function
232 found in modern operating systems. It's based on the implementation found on FreeBSD 2.2.[56]-RELEASE.
233 This function is also exported by default.
234  
235 For both functions, if a salt value is not supplied, a random salt will be
236 generated, using the function random_md5_salt().
237 This function is not exported by default.
238  
239 =head1 METHODS
240  
241 =head2 apache_md5_crypt($password, $salt)
242  
243 This sets a magic variable, and then passes all the calling parameters to L</unix_md5_crypt($password, $salt)>.
244  
245 Returns an encrypted version of the given password.
246  
247 Basically, it's a very poor choice for anything other than password authentication.
248  
249 =head2 random_md5_salt([$length])
250  
251 Here, [] indicate an optional parameter.
252  
253 Returns a random salt of the given length.
254  
255 The maximum length is 8.
256  
257 If C<$length> is omitted, it defaults to 8.
258  
259 =head2 unix_md5_crypt($password, $salt)
260  
261 Returns an encrypted version of the given password.
262  
263 Basically, it's a very poor choice for anything other than password authentication.
264  
265 =head1 Repository
266  
267 L<https://github.com/ronsavage/Crypt-PasswdMD5.git>
268  
269 =head1 SUPPORT
270  
271 Bugs should be reported via the CPAN bug tracker at
272  
273 L<https://github.com/ronsavage/Crypt-PasswdMD5/issues>
274  
275 =head1 LICENSE, AND DISCLAIMER
276  
277 See the accompanying LICENSE file.
278  
279 This program is distributed in the hope that it will be useful, but
280 without any warranty; without even the implied warranty of
281 merchantability or fitness for a particular purpose.
282  
283 =head1 AUTHOR
284  
285 Luis E. Muñoz <luismunoz@cpan.org>.
286  
287 Maintenance by Ron Savage <rsavage@cpan.org> as of V 1.40.
288  
289 =cut
↑ Summary