blib/lib/Digest/Bcrypt.pm

filter: Statementstmt 100% 107 / 107 Branchbran 90.4% 38 / 42 Conditioncond 88.8% 8 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 20 / 20 Podpod 100% 10 / 10 totaltotal 96.3% 187 / 194 CC 25 SCAR 12.8
CC
25
Cov
97%
new
8.0
settings
4.0
_check_cost
4.0
CRAP 3.6
↑ Summary
1 package Digest::Bcrypt;
2 4 ▶use parent 'Digest::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 require bytes;
7  
8 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Crypt::Bcrypt qw(bcrypt bcrypt_check);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use MIME::Base64 qw(decode_base64 encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 our $VERSION = '1.212';
14  
15 22 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod documented
16 22 my $self = shift;
17 22 $self->{_buffer} .= join('', @_);
18 22 return $self;
19 }
20  
21 2 ▶sub bcrypt_b64digest {
Subroutine bcrypt_b64digest: called CC=1 SCAR=0.0
Pod documented
22 2 my $encoded = encode_base64(shift->digest, "");
23 2 $encoded =~ tr{A-Za-z0-9+/=}{./A-Za-z0-9}d;
24 2 return $encoded;
25 }
26  
27 2 ▶sub clone {
Subroutine clone: called CC=2 SCAR=7.0
Pod documented
28 2 my $self = shift;
29 return undef
30 2 ▶ unless my $clone = $self->new(
Branch: 1 branch
BranchTrueFalseTotal
unless my $clone = $self->new("cost", $self->cost, "salt", $self->salt, "type", $self->type) 0 2 2
31 cost => $self->cost,
32 salt => $self->salt,
33 type => $self->type,
34 );
35 2 $clone->add($self->{_buffer});
36 2 return $clone;
37 }
38  
39 114 ▶sub cost {
Subroutine cost: called CC=3 SCAR=11.0
Pod documented
40 114 my $self = shift;
41 114 ▶ return $self->{cost} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 70 44 114
42  
43 44 my $cost = shift;
44  
45 # allow and undefined value to clear it
46 44 ▶ unless (defined $cost) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cost) 2 42 44
47 2 delete $self->{cost};
48 2 return $self;
49 }
50 42 $self->_check_cost($cost);
51  
52 # bcrypt requires 2 digit costs, it dies if it's a single digit.
53 41 $self->{cost} = sprintf("%02d", $cost);
54 41 return $self;
55 }
56  
57 16 ▶sub digest {
Subroutine digest: called CC=2 SCAR=7.0
Pod documented
58 16 my $self = shift;
59 16 $self->_check_cost;
60 15 $self->_check_salt;
61  
62 14 ▶ my $type = defined($self->{type}) ? $self->{type} : '2a';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'type'} ? : 0 14 14
63 14 my $hash = bcrypt($self->{_buffer}, $type, $self->cost, $self->salt);
64 12 my $settings = $self->settings;
65 12 $self->reset;
66 12 return _de_base64(substr($hash, length($settings)));
67 }
68  
69 # new isn't actually implemented in the base class. eww.
70 39 ▶sub new {
Subroutine new: called CC=8 SCAR=20.8
Pod documented
71 39 my $class = shift;
72 39 ▶ my $self = bless {_buffer => '',}, ref $class || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
2370
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
73 39 ▶ return $self unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 11 28 39
74 28 ▶ my $params = @_ > 1 ? {@_} : {%{$_[0]}};
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 12 16 28
75 24 ▶ $self->cost($params->{cost}) if $params->{cost};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"cost"} 14 10 24
76 24 ▶ $self->salt($params->{salt}) if $params->{salt};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"salt"} 14 10 24
77 24 ▶ $self->settings($params->{settings}) if $params->{settings};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"settings"} 8 16 24
78 24 ▶ $self->type($params->{type}) if $params->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"type"} 0 24 24
79 24 return $self;
80 }
81  
82 22 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
83 22 my $self = shift;
84 22 $self->{_buffer} = '';
85 22 delete $self->{cost};
86 22 delete $self->{salt};
87 22 delete $self->{type};
88 22 return $self;
89 }
90  
91 132 ▶sub salt {
Subroutine salt: called CC=3 SCAR=11.0
Pod documented
92 132 my $self = shift;
93 132 ▶ return $self->{salt} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 90 42 132
94  
95 42 my $salt = shift;
96  
97 # allow and undefined value to clear it
98 42 ▶ unless (defined $salt) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $salt) 2 40 42
99 2 delete $self->{salt};
100 2 return $self;
101 }
102  
103 # all other values go through the check
104 40 $self->_check_salt($salt);
105 38 $self->{salt} = $salt;
106 38 return $self;
107 }
108  
109 31 ▶sub settings {
Subroutine settings: called CC=4 SCAR=13.9
Pod documented
110 31 my $self = shift;
111 31 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 20 11 31
112 20 my $cost = sprintf('%02d', $self->{cost});
113 20 my $salt_base64 = encode_base64($self->salt, "");
114 20 $salt_base64 =~ tr{A-Za-z0-9+/=}{./A-Za-z0-9}d;
115 20 ▶ my $type = defined($self->{type}) ? $self->{type} : '2a';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'type'} ? : 8 12 20
116 20 return "\$${type}\$${cost}\$${salt_base64}";
117 }
118 11 my $settings = shift;
119 11 ▶ Carp::croak "bad bcrypt settings"
Branch: 1 branch
BranchTrueFalseTotal
unless $settings =~ m[\A\$(2[abxy])\$([0-9]{2})\$ ([./A-Za-z0-9]{22})]x 3 8 11
120 unless $settings =~ m#\A\$(2[abxy])\$([0-9]{2})\$
121 ([./A-Za-z0-9]{22})#x;
122 8 my ($type, $cost, $salt_base64) = ($1, $2, $3);
123 8 $self->type($type);
124 8 $self->cost($cost);
125 8 $self->salt(_de_base64($salt_base64));
126 8 return $self;
127 }
128  
129 10 ▶sub type {
Subroutine type: called CC=3 SCAR=11.0
Pod documented
130 10 my $self = shift;
131 10 ▶ return $self->{type} unless (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 8 10
132  
133 8 my $type = shift;
134 8 ▶ Carp::croak "bad bcrypt type" unless $type =~ /^2[abxy]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^2[abxy]$/ 0 8 8
135 8 $self->{type} = $type;
136 8 return $self;
137 }
138  
139 # Checks that the cost is a positive integerCroaks if it isn't
140 58 ▶sub _check_cost {
Subroutine _check_cost: called CC=4 SCAR=13.9
141 58 my ($self, $cost) = @_;
142 58 ▶ $cost = defined $cost ? $cost : $self->cost;
Branch: 1 branch
BranchTrueFalseTotal
defined $cost ? : 42 16 58
143 58 ▶ if (!defined $cost || $cost !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cost && $cost =~ /^\d+$/) 2 56 58
Condition100%3/3
defined $cost && $cost =~ /^\d+$/
!ll&&!rl&&r
1156
MC/DC100%2/2
defined $cost && $cost =~ /^\d+$/
defined $cost $cost =~ /^\d+$/
Truth table100%3/3
A && B
A= defined $costB= $cost =~ /^\d+$/
ABresult
0X0
100
111
144 2 Carp::croak "Cost must be a positive integer";
145 }
146 }
147  
148 # Checks that the salt exactly 16 octets long. Croaks if it isn't
149 55 ▶sub _check_salt {
Subroutine _check_salt: called CC=4 SCAR=13.9
150 55 my ($self, $salt) = @_;
151 55 ▶ $salt = defined $salt ? $salt : $self->salt;
Branch: 1 branch
BranchTrueFalseTotal
defined $salt ? : 40 15 55
152 55 ▶ unless ($salt && bytes::length($salt) == 16) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($salt and &bytes::length($salt) == 16) 3 52 55
Condition100%3/3
$salt and &bytes::length($salt) == 16
!ll&&!rl&&r
1252
MC/DC100%2/2
$salt and &bytes::length($salt) == 16
$salt &bytes::length($salt) == 16
Truth table100%3/3
A and B
A= $saltB= &bytes::length($salt) == 16
ABresult
0X0
100
111
153 3 Carp::croak "Salt must be exactly 16 octets long";
154 }
155 }
156  
157 20 ▶sub _de_base64 {
Subroutine _de_base64: called CC=1 SCAR=0.0
158 20 my ($text) = @_;
159 20 $text =~ tr#./A-Za-z0-9#A-Za-z0-9+/#;
160 20 return decode_base64($text);
161 }
162  
163  
164 4 1;
165  
166 =encoding utf8
167  
168 =head1 NAME
169  
170 Digest::Bcrypt - Perl interface to the bcrypt digest algorithm
171  
172 =head1 SYNOPSIS
173  
174 #!/usr/bin/env perl
175 use strict;
176 use warnings;
177 use utf8;
178 use Digest; # via the Digest module (recommended)
179  
180 my $bcrypt = Digest->new('Bcrypt', cost => 12, salt => 'abcdefgh♥stuff');
181 # You can forego the cost and salt in favor of settings strings:
182 my $bcrypt = Digest->new('Bcrypt', settings => '$2a$20$GA.eY03tb02ea0DqbA.eG.');
183  
184 # $cost is an integer between 5 and 31
185 $bcrypt->cost(12);
186  
187 # $type is a selection between 2a, 2b, 2x, and 2y
188 $bcrypt->type('2b');
189  
190 # $salt must be exactly 16 octets long
191 $bcrypt->salt('abcdefgh♥stuff');
192 # OR, for good, random salts:
193 use Data::Entropy::Algorithms qw(rand_bits);
194 $bcrypt->salt(rand_bits(16*8)); # 16 octets
195  
196 # You can forego the cost and salt in favor of settings strings:
197 $bcrypt->settings('$2a$20$GA.eY03tb02ea0DqbA.eG.');
198  
199 # add some strings we want to make a secret of
200 $bcrypt->add('some stuff', 'here and', 'here');
201  
202 my $digest = $bcrypt->digest;
203 $digest = $bcrypt->hexdigest;
204 $digest = $bcrypt->b64digest;
205  
206 # bcrypt's own non-standard base64 dictionary
207 $digest = $bcrypt->bcrypt_b64digest;
208  
209 # Now, let's create a password hash and check it later:
210 use Data::Entropy::Algorithms qw(rand_bits);
211 my $bcrypt = Digest->new('Bcrypt', type => '2b', cost => 20, salt => rand_bits(16*8));
212 my $settings = $bcrypt->settings(); # save for later checks.
213 my $pass_hash = $bcrypt->add('Some secret password')->digest;
214  
215 # much later, we can check a password against our hash via:
216 my $bcrypt = Digest->new('Bcrypt', settings => $settings);
217 if ($bcrypt->add($value_from_user)->digest eq $known_pass_hash) {
218 say "Your password matched";
219 }
220 else {
221 say "Try again!";
222 }
223  
224 # Now that you've seen how cumbersome/silly that is,
225 # please use Crypt::Bcrypt instead of this module.
226  
227 =head1 NOTICE
228  
229 While maintenance for L<Digest::Bcrypt> will continue, there's no reason to use
230 L<Digest::Bcrypt> when L<Crypt::Bcrypt> already exists. We strongly suggest
231 that you use L<Crypt::Bcrypt> instead.
232  
233 This C<Digest::Bcrypt> interface is crufty and laborious to use when compared
234 to that of L<Crypt::Bcrypt>.
235  
236 =head1 DESCRIPTION
237  
238 L<Digest::Bcrypt> provides a L<Digest>-based interface to the
239 L<Crypt::Bcrypt> library.
240  
241 Please note that you B<must> set a C<salt> of exactly 16 octets in length,
242 and you B<must> provide a C<cost> in the range C<1..31>.
243  
244 =head1 ATTRIBUTES
245  
246 L<Digest::Bcrypt> implements the following attributes.
247  
248 =head2 cost
249  
250 $bcrypt = $bcrypt->cost(20); # allows for method chaining
251 my $cost = $bcrypt->cost();
252  
253 An integer in the range C<5..31>, this is required.
254  
255 See L<Crypt::Eksblowfish::Bcrypt> for a detailed description of C<cost>
256 in the context of the bcrypt algorithm.
257  
258 When called with no arguments, it will return the current cost.
259  
260 =head2 salt
261  
262 $bcrypt = $bcrypt->salt('abcdefgh♥stuff'); # allows for method chaining
263 my $salt = $bcrypt->salt();
264  
265 # OR, for good, random salts:
266 use Data::Entropy::Algorithms qw(rand_bits);
267 $bcrypt->salt(rand_bits(16*8)); # 16 octets
268  
269 Sets the value to be used as a salt. Bcrypt requires B<exactly> 16 octets of salt.
270  
271 It is recommenced that you use a module like L<Data::Entropy::Algorithms> to
272 provide a truly randomized salt.
273  
274 When called with no arguments, it will return the current salt.
275  
276 =head2 settings
277  
278 $bcrypt = $bcrypt->settings('$2a$20$GA.eY03tb02ea0DqbA.eG.'); # allows for method chaining
279 my $settings = $bcrypt->settings();
280  
281 A C<settings> string can be used to set the L<Digest::Bcrypt/salt> and
282 L<Digest::Bcrypt/cost> automatically. Setting the C<settings> will override any
283 current values in your C<cost> and C<salt> attributes.
284  
285 For details on the C<settings> string requirements, please see L<Crypt::Eksblowfish::Bcrypt>.
286  
287 When called with no arguments, it will return the current settings string.
288  
289 =head2 type
290  
291 $bcrypt = $bcrypt->type('2b');
292 # method chaining on mutations
293 say $bcrypt->type(); # 2b
294  
295 This sets the subtype of bcrypt used. These subtypes are as defined in L<Crypt::Bcrypt>.
296 The available types are:
297 C<2b> which is the current standard,
298 C<2a> which is older; it's the one used in L<Crypt::Eksblowfish>,
299 C<2y> which is considered equivalent to C<2b> and used in PHP.
300 C<2x> which is very broken and only needed to work with ancient PHP versions.
301  
302 =head1 METHODS
303  
304 L<Digest::Bcrypt> inherits all methods from L<Digest::base> and implements/overrides
305 the following methods as well.
306  
307 =head2 new
308  
309 my $bcrypt = Digest->new('Bcrypt', %params);
310 my $bcrypt = Digest::Bcrypt->new(%params);
311 my $bcrypt = Digest->new('Bcrypt', \%params);
312 my $bcrypt = Digest::Bcrypt->new(\%params);
313  
314 Creates a new C<Digest::Bcrypt> object. It is recommended that you use the L<Digest>
315 module in the first example rather than using L<Digest::Bcrypt> directly.
316  
317 Any of the L<Digest::Bcrypt/ATTRIBUTES> above can be passed in as a parameter.
318  
319 =head2 add
320  
321 $bcrypt->add("a"); $bcrypt->add("b"); $bcrypt->add("c");
322 $bcrypt->add("a")->add("b")->add("c");
323 $bcrypt->add("a", "b", "c");
324 $bcrypt->add("abc");
325  
326 Adds data to the message we are calculating the digest for. All the above
327 examples have the same effect.
328  
329 =head2 b64digest
330  
331 my $digest = $bcrypt->b64digest;
332  
333 Same as L</"digest">, but will return the digest base64 encoded.
334  
335 The C<length> of the returned string will be 31 and will only contain characters
336 from the ranges C<'0'..'9'>, C<'A'..'Z'>, C<'a'..'z'>, C<'+'>, and C<'/'>
337  
338 The base64 encoded string returned is not padded to be a multiple of 4 bytes long.
339  
340 =head2 bcrypt_b64digest
341  
342 my $digest = $bcrypt->bcrypt_b64digest;
343  
344 Same as L</"digest">, but will return the digest base64 encoded using the alphabet
345 that is commonly used with bcrypt.
346  
347 The C<length> of the returned string will be 31 and will only contain characters
348 from the ranges C<'0'..'9'>, C<'A'..'Z'>, C<'a'..'z'>, C<'+'>, and C<'.'>
349  
350 The base64 encoded string returned is not padded to be a multiple of 4 bytes long.
351  
352 I<Note:> This is bcrypt's own non-standard base64 alphabet, It is B<not>
353 compatible with the standard MIME base64 encoding.
354  
355 =head2 clone
356  
357 my $clone = $bcrypt->clone;
358  
359 Creates a clone of the C<Digest::Bcrypt> object, and returns it.
360  
361 =head2 digest
362  
363 my $digest = $bcrypt->digest;
364  
365 Returns the binary digest for the message. The returned string will be 23 bytes long.
366  
367 =head2 hexdigest
368  
369 my $digest = $bcrypt->hexdigest;
370  
371 Same as L</"digest">, but will return the digest in hexadecimal form.
372  
373 The C<length> of the returned string will be 46 and will only contain
374 characters from the ranges C<'0'..'9'> and C<'a'..'f'>.
375  
376 =head2 reset
377  
378 $bcrypt->reset;
379  
380 Resets the object to the same internal state it was in when it was constructed.
381  
382 =head1 SEE ALSO
383  
384 L<Digest>, L<Crypt::Eksblowfish::Bcrypt>, L<Data::Entropy::Algorithms>
385  
386 =head1 AUTHOR
387  
388 James Aitken C<jaitken@cpan.org>
389  
390 =head1 CONTRIBUTORS
391  
392 =over
393  
394 =item *
395  
396 Chase Whitener C<capoeira@cpan.org>
397  
398 =back
399  
400 =head1 COPYRIGHT AND LICENSE
401  
402 This software is copyright (c) 2012 by James Aitken.
403  
404 This is free software; you can redistribute it and/or modify it under
405 the same terms as the Perl 5 programming language system itself.
406  
407 =cut
↑ Summary