blib/lib/Dancer/Plugin/Bcrypt.pm

filter: Statementstmt 91.4% 43 / 47 Branchbran 58.3% 7 / 12 Conditioncond 70.0% 7 / 10 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 2 totaltotal 80.2% 69 / 86 CC 11 SCAR 12.1
CC
11
Cov
83%
sanity_check
5.3
bcrypt
4.0
bcrypt_validate_password
3.8
CRAP 3.3
↑ Summary
1 package Dancer::Plugin::Bcrypt;
2  
3 # ABSTRACT: DEPRECATED Bcrypt interface for Dancer
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Dancer::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Crypt::Eksblowfish::Bcrypt qw/en_base64/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Crypt::Random::Source;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = '0.4.1';
14  
15  
16 2 register bcrypt => \&bcrypt;
17 2 register bcrypt_validate_password => \&bcrypt_validate_password;
18  
19  
20 2 ▶sub bcrypt {
Subroutine bcrypt: called CC=4 SCAR=13.9
21 2 my ($plaintext, $bcrypted) = @_;
22  
23 2 ▶ return if !$plaintext;
Branch: 1 branch
BranchTrueFalseTotal
unless $plaintext 0 2 2
24  
25 # Sanity checks, and provide some good defaults.
26 2 my $config = sanity_check();
27  
28 # On to the actual work...
29  
30 # If you pass a plaintext password and an bcrypted one (from a DB f.ex)
31 # we hash the plaintext password using the same method, salt and
32 # work factor as the stored version. If the plaintext password matches
33 # the stored version then the resulting hashes should be identical.
34  
35 2 ▶ if ($bcrypted && $bcrypted =~ /^\$2a\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bcrypted and $bcrypted =~ /^\$2a\$/) 1 1 2
Condition66%2/3
$bcrypted and $bcrypted =~ /^\$2a\$/
!ll&&!rl&&r
101
MC/DC50%1/2
$bcrypted and $bcrypted =~ /^\$2a\$/
$bcrypted $bcrypted =~ /^\$2a\$/
Truth table66%2/3
A and B
A= $bcryptedB= $bcrypted =~ /^\$2a\$/
ABresult
0X0
100
111
36 1 return Crypt::Eksblowfish::Bcrypt::bcrypt($plaintext, $bcrypted);
37 }
38  
39 # If we have been passed only the plaintext, then we
40 # generate the bcrypted version with all new settings
41  
42 # Use bcrypt and append with a NULL - The accepted way to do it
43 1 my $method = '$2a';
44  
45 # Has to be 2 digits exactly
46 1 my $work_factor = sprintf("%02d", $config->{work_factor});
47  
48 # Salt must be exactly 16 octets, base64 encoded.
49 1 my $salt = en_base64( generate_salt( $config->{random_factor} ) );
50  
51 # Create the settings string that we will use to bcrypt the plaintext
52 # Read the docs of the Crypt:: modules for an explanation of this string
53 1 my $new_settings = join('$', $method, $work_factor, $salt);
54  
55  
56 1 return Crypt::Eksblowfish::Bcrypt::bcrypt($plaintext, $new_settings);
57 };
58  
59  
60 1 ▶sub bcrypt_validate_password {
Subroutine bcrypt_validate_password: called CC=3 SCAR=13.3
61 1 my ($plaintext, $bcrypted) = @_;
62  
63 1 ▶ if ($plaintext && $bcrypted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plaintext and $bcrypted) { } 1 0 1
Condition33%1/3
$plaintext and $bcrypted
!ll&&!rl&&r
001
MC/DC0%0/2
$plaintext and $bcrypted
$plaintext $bcrypted
Truth table33%1/3
A and B
A= $plaintextB= $bcrypted
ABresult
0X0
100
111
64 1 return bcrypt($plaintext, $bcrypted) eq $bcrypted;
65 } else {
66 0 return;
67 }
68 }
69  
70  
71 2 ▶sub sanity_check {
Subroutine sanity_check: called CC=5 SCAR=16.7
Pod undocumented
72 2 my $config = plugin_setting;
73  
74 # Takes ~0.007 seconds on 2011 hardware
75 2 ▶ $config->{work_factor} ||= 4;
Condition100%2/2
$config->{"work_factor"} ||= 4
l!l
11
MC/DC100%1/1
$config->{"work_factor"} ||= 4
$config->{"work_factor"}
Truth table100%2/2
A
A= $config->{"work_factor"}
Aresult
11
00
76  
77 # Uses /dev/urandom - which is pretty good
78 2 ▶ $config->{random_factor} ||= 'weak';
Condition100%2/2
$config->{"random_factor"} ||= "weak"
l!l
11
MC/DC100%1/1
$config->{"random_factor"} ||= "weak"
$config->{"random_factor"}
Truth table100%2/2
A
A= $config->{"random_factor"}
Aresult
11
00
79  
80 # Work factors higher than 31 aren't supported.
81 2 ▶ if ($config->{work_factor} > 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"work_factor"} > 31) 0 2 2
82 0 $config->{work_factor} = 31;
83 };
84  
85 # Can only specify weak or strong as random_factor
86 2 ▶ unless ( grep { $_ eq $config->{random_factor} } ('strong', 'weak') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $config->{"random_factor"};} "strong", "weak") 0 2 2
87 0 $config->{random_factor} = 'weak';
88 }
89  
90 return {
91 work_factor => $config->{work_factor},
92 random_factor => $config->{random_factor},
93 2 };
94 }
95  
96  
97 1 ▶sub generate_salt {
Subroutine generate_salt: called CC=2 SCAR=7.6
Pod undocumented
98 1 my ($type) = @_;
99  
100 1 ▶ if ($type eq 'strong') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'strong') { } 0 1 1
101 0 return Crypt::Random::Source::get_strong(16);
102 } else {
103 1 return Crypt::Random::Source::get_weak(16);
104 }
105 }
106  
107  
108 2 register_plugin;
109  
110 2 1;
111  
112  
113 =pod
114  
115 =head1 NAME
116  
117 Dancer::Plugin::Bcrypt - DEPRECATED Bcrypt interface for Dancer
118  
119  
120 =head1 VERSION
121  
122 version 0.4.1
123  
124  
125 =head1 DESCRIPTION
126  
127 PLEASE NOTE THAT WHILE THIS MODULE WORKS, IT IS DEPRECATED, AND NO LONGER MAINTAINED.
128  
129 I suggest you use the more flexible replacement L<Dancer::Plugin::Passphrase> -
130 It has all the same functionality as the module, and also allows you to match
131 against other hashing algorithms as well as brcypt.
132  
133 Original documentation continues below...
134  
135 This plugin is a simple interface to the bcrypt algorithm allowing web apps
136 created by dancer to easily store passwords in a secure way.
137  
138 It generates a crypographically strong salt for each password, uses the
139 very strong bcrypts algorithm to hash the password - and does these in a
140 configurable and portable manner.
141  
142  
143 =head1 BACKGROUND
144  
145 See L<http://codahale.com/how-to-safely-store-a-password/>
146  
147 To safely store passwords in the modern era, you should use bcrypt.
148 It's that simple
149  
150 MD5, SHA and their ilk are general purpose hash functions, designed for speed.
151  
152 An average server can calculate the MD5 hash of every 6 character, alphanumeric
153 password in about 40 seconds. The beefiest boxen can do the same in ONE second
154  
155 Bcrypt is an adaptive password hashing algorithm. It uses a work factor
156 to determine how SLOWLY it hashes a password. This work factor
157 can be increased to keep up with the ever increasing power of computers.
158  
159  
160 =head1 KEYWORDS
161  
162 =head2 bcrypt
163  
164 Pass it a plaintext password, and it will return a string suitable for
165 storage, using the settings specified in the app config.
166  
167 This string contains the bcrypted hash, work factor used, and the salt used
168 to generate the hash, delimited by a $.
169  
170 my $hash = bcrypt($plaintext);
171  
172 Pass a plaintext password and a stored bcrypted string, it will return a hash
173 of the plaintext password using the work factor and salt from the stored hash.
174  
175 You would use this to verify that a password provided by a user matches the
176 hash you have stored in the database.
177  
178 my $hash = bcrypt($plaintext, $stored_hash);
179  
180 =head2 bcrypt_validate_password
181  
182 Pass it a plaintext password and the crypted password you have stored, and it
183 will return a boolean to indicate whether the plaintext password entered is
184 correct (it hashes to the same has the stored hash).
185  
186 if (bcrypt_validate_password($entered_password, $stored_hash)) {
187 ...
188 }
189  
190  
191 =head1 USAGE
192  
193 package MyWebService;
194 use Dancer;
195 use Dancer::Plugin::Bcrypt;
196  
197 get '/' sub => {
198  
199 # Generate a new hashed password - suitable for storing in a DB.
200 my $hash = bcrypt( param('password') );
201  
202 # [...]
203  
204 # Validate password provided by user against stored hash.
205 my $stored_hash = ''; # [...] retreive password from the DB.
206  
207 if (bcrypt_validate_password(param('password'), $stored_hash)) {
208 # Entered password matches
209 }
210  
211 };
212  
213  
214 =head1 CONFIGURATION
215  
216 You can set the work factor and the random-ness of the salt in your config.yml
217  
218 plugins:
219 bcrypt:
220 work_factor: 8
221 random_factor: strong
222  
223  
224 =head1 SEE ALSO
225  
226 L<Dancer>, L<Crypt::Eksblowfish::Bcrypt>, L<Crypt::Random::Source>,
227 L<http://codahale.com/how-to-safely-store-a-password/>
228  
229  
230 =head1 AUTHOR
231  
232 James Aitken <jaitken@cpan.org>
233  
234  
235 =head1 COPYRIGHT AND LICENSE
236  
237 This software is copyright (c) 2011 by James Aitken.
238  
239 This is free software; you can redistribute it and/or modify it under
240 the same terms as the Perl 5 programming language system itself.
241  
242 =cut
↑ Summary