line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::Bcrypt::Easy; |
2
|
|
|
|
|
|
|
$Crypt::Bcrypt::Easy::VERSION = '2.011006'; |
3
|
1
|
|
|
1
|
|
13924
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
71
|
|
4
|
1
|
|
|
1
|
|
372
|
use strictures 2; |
|
1
|
|
|
|
|
1124
|
|
|
1
|
|
|
|
|
49
|
|
5
|
1
|
|
|
1
|
|
474
|
use App::bmkpasswd 'mkpasswd', 'passwdcmp', 'mkpasswd_forked'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
16
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
284
|
use Scalar::Util 'blessed'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
54
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
3
|
use parent 'Exporter::Tiny'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
6
|
|
10
|
|
|
|
|
|
|
our @EXPORT = 'bcrypt'; |
11
|
8
|
|
|
8
|
1
|
115081
|
sub bcrypt { Crypt::Bcrypt::Easy->new(@_) } |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub DEFAULT_COST () { '08' } |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=pod |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=for Pod::Coverage new DEFAULT_COST |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=cut |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub new { |
22
|
10
|
|
|
10
|
0
|
83
|
my ($cls, %params) = @_; |
23
|
10
|
|
100
|
|
|
48
|
my $cost = $params{cost} || DEFAULT_COST; |
24
|
10
|
100
|
|
|
|
29
|
mkpasswd_forked if $params{reset_seed}; |
25
|
10
|
|
33
|
|
|
94
|
bless \$cost, blessed($cls) || $cls |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub cost { |
29
|
5
|
|
|
5
|
1
|
7
|
my ($self) = @_; |
30
|
5
|
100
|
|
|
|
50
|
blessed $self ? $$self : DEFAULT_COST |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub compare { |
34
|
9
|
|
|
9
|
1
|
48316
|
my ($self, %params) = @_; |
35
|
9
|
50
|
33
|
|
|
55
|
unless (defined $params{text} && defined $params{crypt}) { |
36
|
0
|
|
|
|
|
0
|
confess "Expected 'text =>' and 'crypt =>' params" |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
passwdcmp $params{text} => $params{crypt} |
39
|
9
|
|
|
|
|
33
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub crypt { |
42
|
6
|
|
|
6
|
1
|
733
|
my $self = shift; |
43
|
6
|
|
|
|
|
8
|
my %params; |
44
|
|
|
|
|
|
|
|
45
|
6
|
100
|
|
|
|
20
|
if (@_ == 1) { |
|
|
50
|
|
|
|
|
|
46
|
4
|
|
|
|
|
10
|
$params{text} = $_[0] |
47
|
|
|
|
|
|
|
} elsif (@_ > 1) { |
48
|
2
|
|
|
|
|
6
|
%params = @_; |
49
|
|
|
|
|
|
|
confess "Expected 'text =>' param" |
50
|
2
|
50
|
|
|
|
7
|
unless defined $params{text}; |
51
|
|
|
|
|
|
|
} else { |
52
|
0
|
|
|
|
|
0
|
confess 'Not enough arguments; expected a password' |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
mkpasswd $params{text} => |
56
|
|
|
|
|
|
|
($params{type} || 'bcrypt'), |
57
|
|
|
|
|
|
|
($params{cost} || $self->cost), |
58
|
6
|
|
50
|
|
|
51
|
($params{strong} || () ) |
|
|
|
66
|
|
|
|
|
|
|
|
33
|
|
|
|
|
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
1; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=pod |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head1 NAME |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Crypt::Bcrypt::Easy - Simple interface to bcrypted passwords |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head1 SYNOPSIS |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
use Crypt::Bcrypt::Easy; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# Generate bcrypted passwords: |
74
|
|
|
|
|
|
|
my $plain = 'my_password'; |
75
|
|
|
|
|
|
|
my $passwd = bcrypt->crypt( $plain ); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# Generate passwords with non-default options: |
78
|
|
|
|
|
|
|
my $passwd = bcrypt->crypt( text => $plain, cost => 10 ); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# Compare passwords: |
81
|
|
|
|
|
|
|
if (bcrypt->compare( text => $plain, crypt => $passwd )) { |
82
|
|
|
|
|
|
|
# Successful match |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# Spawn a new instance that will generate passwords using a different |
86
|
|
|
|
|
|
|
# default workcost: |
87
|
|
|
|
|
|
|
my $bc = bcrypt( cost => 10 ); |
88
|
|
|
|
|
|
|
my $passwd = $bc->crypt( $plain ); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# Without imported constructor: |
91
|
|
|
|
|
|
|
use Crypt::Bcrypt::Easy (); |
92
|
|
|
|
|
|
|
my $passwd = Crypt::Bcrypt::Easy->crypt( text => $plain, cost => 10 ) |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 DESCRIPTION |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
This module provides an easy interface to creating and comparing bcrypt-hashed |
97
|
|
|
|
|
|
|
passwords via L's exported helpers (which were created to |
98
|
|
|
|
|
|
|
power C and are a bit awkward to use directly). |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
This POD briefly covers usage of this interface; see L for |
101
|
|
|
|
|
|
|
more details on bcrypt, internals, and documentation regarding the more |
102
|
|
|
|
|
|
|
flexible functional interface. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
This module uses L; you can rename the L function |
105
|
|
|
|
|
|
|
as-needed: |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
use Crypt::Bcrypt::Easy 'bcrypt' => { -as => 'bc' }; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head2 bcrypt |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
my $bcrypt = bcrypt( cost => 10 ); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Creates and returns a new Crypt::Bcrypt::Easy object. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
The default C is '08'. This can be also be tuned for individual runs; |
116
|
|
|
|
|
|
|
see L. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
(This is merely a convenience function for calling C<< |
119
|
|
|
|
|
|
|
Crypt::Bcrypt::Easy->new >>.) |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
If your application generates passwords in multiple child processes or |
122
|
|
|
|
|
|
|
threads, you can cause L to be automatically |
123
|
|
|
|
|
|
|
called during object construction in each individual process by specifying the |
124
|
|
|
|
|
|
|
C option: |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
my $bcrypt = bcrypt( reset_seed => 1, cost => 8 ); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
(The C option was added in C.) |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head3 crypt |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Create and return a new password hash: |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
my $crypted = bcrypt->crypt( 'my_password' ); |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Override default options (see L): |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
my $crypted = bcrypt->crypt( |
139
|
|
|
|
|
|
|
text => 'my_password', |
140
|
|
|
|
|
|
|
cost => 10, |
141
|
|
|
|
|
|
|
strong => 1, |
142
|
|
|
|
|
|
|
); |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Specifying a boolean true 'strong =>' parameter enables strongly-random salts |
145
|
|
|
|
|
|
|
(see L). |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head3 compare |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
if (bcrypt->compare(text => 'my_password', crypt => $crypted)) { |
150
|
|
|
|
|
|
|
... |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Returns boolean true if hashes match. Accepts any type of hash supported by |
154
|
|
|
|
|
|
|
L and your system; see L. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head3 cost |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Returns the current work-cost value; see L. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 AUTHOR |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Jon Portnoy |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=cut |