| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Crypt::SaltedHash; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
189806
|
use v5.6.0; |
|
|
2
|
|
|
|
|
7
|
|
|
4
|
2
|
|
|
2
|
|
12
|
use strict; |
|
|
2
|
|
|
|
|
7
|
|
|
|
2
|
|
|
|
|
68
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
1045
|
use Crypt::SysRandom (); |
|
|
2
|
|
|
|
|
8237
|
|
|
|
2
|
|
|
|
|
99
|
|
|
7
|
2
|
|
|
2
|
|
1286
|
use Digest (); |
|
|
2
|
|
|
|
|
1567
|
|
|
|
2
|
|
|
|
|
96
|
|
|
8
|
2
|
|
|
2
|
|
1105
|
use MIME::Base64 (); |
|
|
2
|
|
|
|
|
1729
|
|
|
|
2
|
|
|
|
|
58
|
|
|
9
|
2
|
|
|
2
|
|
1206
|
use POSIX (); |
|
|
2
|
|
|
|
|
18481
|
|
|
|
2
|
|
|
|
|
3444
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.12'; |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=encoding latin1 |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 NAME |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Crypt::SaltedHash - Perl interface to functions that assist in working |
|
18
|
|
|
|
|
|
|
with salted hashes. |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
use Crypt::SaltedHash; |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my $csh = Crypt::SaltedHash->new(algorithm => 'SHA-1'); |
|
25
|
|
|
|
|
|
|
$csh->add('secret'); |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $salted = $csh->generate; |
|
28
|
|
|
|
|
|
|
my $valid = Crypt::SaltedHash->validate($salted, 'secret'); |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 STATUS |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
This module is deprecated. |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
This module has not had significant updates since 2006. |
|
36
|
|
|
|
|
|
|
There are newer modules that support more secure algorithms and hashing options, |
|
37
|
|
|
|
|
|
|
and are extensible, such as L. |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
You can use the L validator to migrate to more secure algorithms. |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
The C module provides an object oriented interface to |
|
44
|
|
|
|
|
|
|
create salted (or seeded) hashes of clear text data. The original |
|
45
|
|
|
|
|
|
|
formalization of this concept comes from RFC-3112 and is extended by the use |
|
46
|
|
|
|
|
|
|
of different digital algorithms. |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 ABSTRACT |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head2 Setting the data |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
The process starts with 2 elements of data: |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=over |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=item * |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
a clear text string (this could represent a password for instance). |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=item * |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
the salt, a random seed of data. This is the value used to augment a hash in order to |
|
63
|
|
|
|
|
|
|
ensure that 2 hashes of identical data yield different output. |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=back |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
For the purposes of this abstract we will analyze the steps within code that perform the necessary actions |
|
68
|
|
|
|
|
|
|
to achieve the endresult hashes. Cryptographers call this hash a digest. We will not however go into an explanation |
|
69
|
|
|
|
|
|
|
of a one-way encryption scheme. Readers of this abstract are encouraged to get information on that subject by |
|
70
|
|
|
|
|
|
|
their own. |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Theoretically, an implementation of a one-way function as an algorithm takes input, and provides output, that are both |
|
73
|
|
|
|
|
|
|
in binary form; realistically though digests are typically encoded and stored in a database or in a flat text or XML file. |
|
74
|
|
|
|
|
|
|
Take slappasswd5 for instance, it performs the exact functionality described above. We will use it as a black box compiled |
|
75
|
|
|
|
|
|
|
piece of code for our analysis. |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
In pseudocode we generate a salted hash as follows: |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Get the source string and salt as separate binary objects |
|
80
|
|
|
|
|
|
|
Concatenate the 2 binary values |
|
81
|
|
|
|
|
|
|
Hash the concatenation into SaltedPasswordHash |
|
82
|
|
|
|
|
|
|
Base64Encode(concat(SaltedPasswordHash, Salt)) |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
We take a clear text string and hash this into a binary object representing the hashed value of the clear text string plus the random salt. |
|
85
|
|
|
|
|
|
|
Then we have the Salt value, which are typically 4 bytes of purely random binary data represented as hexadecimal notation (Base16 as 8 bytes). |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Using SHA-1 as the hashing algorithm, SaltedPasswordHash is of length 20 (bytes) in raw binary form |
|
88
|
|
|
|
|
|
|
(40 bytes if we look at it in hex). Salt is then 4 bytes in raw binary form. The SHA-1 algorithm generates |
|
89
|
|
|
|
|
|
|
a 160 bit hash string. Consider that 8 bits = 1 byte. So 160 bits = 20 bytes, which is exactly what the |
|
90
|
|
|
|
|
|
|
algorithm gives us. |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
The Base64 encoding of the binary result looks like: |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
{SSHA}B0O0XSYdsk7g9K229ZEr73Lid7HBD9DX |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Take note here that the final output is a 32-byte string of data. The Base64 encoding process uses bit shifting, masking, and padding as per RFC-3548. |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
A couple of examples of salted hashes using on the same exact clear-text string: |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
slappasswd -s testing123 |
|
101
|
|
|
|
|
|
|
{SSHA}72uhy5xc1AWOLwmNcXALHBSzp8xt4giL |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
slappasswd -s testing123 |
|
104
|
|
|
|
|
|
|
{SSHA}zmIAVaKMmTngrUi4UlS0dzYwVAbfBTl7 |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
slappasswd -s testing123 |
|
107
|
|
|
|
|
|
|
{SSHA}Be3F12VVvBf9Sy6MSqpOgAdEj6JCZ+0f |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
slappasswd -s testing123 |
|
110
|
|
|
|
|
|
|
{SSHA}ncHs4XYmQKJqL+VuyNQzQjwRXfvu6noa |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
4 runs of slappasswd against the same clear text string each yielded unique endresult hashes. |
|
113
|
|
|
|
|
|
|
The random salt is generated silently and never made visible. |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head2 Extracting the data |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
One of the keys to note is that the salt is dealt with twice in the process. It is used once for the actual application of randomness to the |
|
118
|
|
|
|
|
|
|
given clear text string, and then it is stored within the final output as purely Base64 encoded data. In order to perform an authentication |
|
119
|
|
|
|
|
|
|
query for instance, we must break apart the concatenation that was created for storage of the data. We accomplish this by splitting |
|
120
|
|
|
|
|
|
|
up the binary data we get after Base64 decoding the stored hash. |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
In pseudocode we would perform the extraction and verification operations as such: |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Strip the hash identifier from the Digest |
|
125
|
|
|
|
|
|
|
Base64Decode(Digest, 20) |
|
126
|
|
|
|
|
|
|
Split Digest into 2 byte arrays, one for bytes 0 � 20(pwhash), one for bytes 21 � 32 (salt) |
|
127
|
|
|
|
|
|
|
Get the target string and salt as separate binary object |
|
128
|
|
|
|
|
|
|
Concatenate the 2 binary values |
|
129
|
|
|
|
|
|
|
SHA hash the concatenation into targetPasswordHash |
|
130
|
|
|
|
|
|
|
Compare targetPasswordHash with pwhash |
|
131
|
|
|
|
|
|
|
Return corresponding Boolean value |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Our job is to split the original digest up into 2 distinct byte arrays, one of the left 20 (0 - 20 including the null terminator) bytes and |
|
134
|
|
|
|
|
|
|
the other for the rest of the data. The left 0 � 20 bytes will represent the salted binary value we will use for a byte-by-byte data |
|
135
|
|
|
|
|
|
|
match against the new clear text presented for verification. The string presented for verification will have to be salted as well. The rest |
|
136
|
|
|
|
|
|
|
of the bytes (21 � 32) represent the random salt which when decoded will show the exact hex characters that make up the once randomly |
|
137
|
|
|
|
|
|
|
generated seed. |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
We are now ready to verify some data. Let's start with the 4 hashes presented earlier. We will run them through our code to extract the |
|
140
|
|
|
|
|
|
|
random salt and then using that verify the clear text string hashed by slappasswd. First, let's do a verification test with an erroneous |
|
141
|
|
|
|
|
|
|
password; this should fail the matching test: |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
{SSHA}72uhy5xc1AWOLwmNcXALHBSzp8xt4giL Test123 |
|
144
|
|
|
|
|
|
|
Hash extracted (in hex): ef6ba1cb9c5cd4058e2f098d71700b1c14b3a7cc |
|
145
|
|
|
|
|
|
|
Salt extracted (in hex): 6de2088b |
|
146
|
|
|
|
|
|
|
Hash length is: 20 Salt length is: 4 |
|
147
|
|
|
|
|
|
|
Hash presented in hex: 256bc48def0ce04b0af90dfd2808c42588bf9542 |
|
148
|
|
|
|
|
|
|
Hashes DON'T match: Test123 |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
The match failure test was successful as expected. Now let's use known valid data through the same exact code: |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
{SSHA}72uhy5xc1AWOLwmNcXALHBSzp8xt4giL testing123 |
|
153
|
|
|
|
|
|
|
Hash extracted (in hex): ef6ba1cb9c5cd4058e2f098d71700b1c14b3a7cc |
|
154
|
|
|
|
|
|
|
Salt extracted (in hex): 6de2088b |
|
155
|
|
|
|
|
|
|
Hash length is: 20 Salt length is: 4 |
|
156
|
|
|
|
|
|
|
Hash presented in hex: ef6ba1cb9c5cd4058e2f098d71700b1c14b3a7cc |
|
157
|
|
|
|
|
|
|
Hashes match: testing123 |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
The process used for salted passwords should now be clear. We see that salting hashed data does indeed add another layer of security to the |
|
160
|
|
|
|
|
|
|
clear text one-way hashing process. But we also see that salted hashes should also be protected just as if the data was in clear text form. |
|
161
|
|
|
|
|
|
|
Now that we have seen salted hashes actually work you should also realize that in code it is possible to extract salt values and use them |
|
162
|
|
|
|
|
|
|
for various purposes. Obviously the usage can be on either side of the colored hat line, but the data is there. |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 METHODS |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=over 4 |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item B |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Returns a new Crypt::SaltedHash object. |
|
171
|
|
|
|
|
|
|
Possible keys for I<%options> are: |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=over |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=item * |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
I: It's also possible to use common string representations of the |
|
178
|
|
|
|
|
|
|
algorithm (e.g. "sha256", "SHA-384"). If the argument is missing, SHA-1 will |
|
179
|
|
|
|
|
|
|
be used by default. |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=item * |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
I: You can specify your on salt. You can either specify it as a sequence |
|
184
|
|
|
|
|
|
|
of characters or as a hex encoded string of the form "HEX{...}". If the argument is missing, |
|
185
|
|
|
|
|
|
|
a random seed is provided for you (recommended). |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=item * |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
I: By default, the module assumes a salt length of 4 bytes (or 8, if it is encoded in hex). |
|
190
|
|
|
|
|
|
|
If you choose a different length, you have to tell the I function how long your seed was. |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=back |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=cut |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub new { |
|
197
|
9
|
|
|
9
|
1
|
240958
|
my ( $class, %options ) = @_; |
|
198
|
|
|
|
|
|
|
|
|
199
|
9
|
|
100
|
|
|
41
|
$options{algorithm} ||= 'SHA-1'; |
|
200
|
9
|
|
100
|
|
|
44
|
$options{salt_len} ||= 4; |
|
201
|
9
|
|
66
|
|
|
55
|
$options{salt} ||= &__generate_hex_salt( $options{salt_len} * 2 ); |
|
202
|
|
|
|
|
|
|
|
|
203
|
9
|
|
|
|
|
30
|
$options{algorithm} = uc( $options{algorithm} ); |
|
204
|
|
|
|
|
|
|
$options{algorithm} .= '-1' |
|
205
|
9
|
50
|
|
|
|
33
|
if $options{algorithm} =~ m!SHA$!; # SHA => SHA-1, HMAC-SHA => HMAC-SHA-1 |
|
206
|
|
|
|
|
|
|
|
|
207
|
9
|
|
|
|
|
49
|
my $digest = Digest->new( $options{algorithm} ); |
|
208
|
|
|
|
|
|
|
my $self = { |
|
209
|
|
|
|
|
|
|
salt => $options{salt}, |
|
210
|
|
|
|
|
|
|
algorithm => $options{algorithm}, |
|
211
|
|
|
|
|
|
|
digest => $digest, |
|
212
|
9
|
|
|
|
|
3561
|
scheme => &__make_scheme( $options{algorithm} ), |
|
213
|
|
|
|
|
|
|
}; |
|
214
|
|
|
|
|
|
|
|
|
215
|
9
|
|
|
|
|
64
|
return bless $self, $class; |
|
216
|
|
|
|
|
|
|
} |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=item B |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
Logically joins the arguments into a single string, and uses it to |
|
221
|
|
|
|
|
|
|
update the current digest state. For more details see L. |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=cut |
|
224
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
sub add { |
|
226
|
10
|
|
|
10
|
1
|
33
|
my $self = shift; |
|
227
|
10
|
|
|
|
|
25
|
$self->obj->add(@_); |
|
228
|
10
|
|
|
|
|
21
|
return $self; |
|
229
|
|
|
|
|
|
|
} |
|
230
|
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=item B |
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
Resets the digest. |
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=cut |
|
236
|
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
sub clear { |
|
238
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
239
|
0
|
|
|
|
|
0
|
$self->{digest} = Digest->new( $self->{algorithm} ); |
|
240
|
0
|
|
|
|
|
0
|
return $self; |
|
241
|
|
|
|
|
|
|
} |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=item B |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
Returns the salt in binary form. |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=cut |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
sub salt_bin { |
|
250
|
10
|
|
|
10
|
1
|
14
|
my $self = shift; |
|
251
|
|
|
|
|
|
|
|
|
252
|
10
|
100
|
|
|
|
94
|
return $self->{salt} =~ m!^HEX\{(.*)\}$!i ? pack( "H*", $1 ) : $self->{salt}; |
|
253
|
|
|
|
|
|
|
} |
|
254
|
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=item B |
|
256
|
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
Returns the salt in hexadecimal form ('HEX{...}') |
|
258
|
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=cut |
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
sub salt_hex { |
|
262
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
263
|
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
return $self->{salt} =~ m!^HEX\{(.*)\}$!i |
|
265
|
|
|
|
|
|
|
? $self->{salt} |
|
266
|
0
|
0
|
|
|
|
0
|
: 'HEX{' . join( '', unpack( 'H*', $self->{salt} ) ) . '}'; |
|
267
|
|
|
|
|
|
|
} |
|
268
|
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=item B |
|
270
|
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
Generates the seeded hash. Uses the I-method of L before actually performing |
|
272
|
|
|
|
|
|
|
the digest calculation, so adding more cleardata after a call of I to an instance of |
|
273
|
|
|
|
|
|
|
I has the same effect as adding the data before the call of I. |
|
274
|
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=cut |
|
276
|
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
sub generate { |
|
278
|
10
|
|
|
10
|
1
|
29
|
my $self = shift; |
|
279
|
|
|
|
|
|
|
|
|
280
|
10
|
|
|
|
|
22
|
my $clone = $self->obj->clone; |
|
281
|
10
|
|
|
|
|
30
|
my $salt = $self->salt_bin; |
|
282
|
|
|
|
|
|
|
|
|
283
|
10
|
|
|
|
|
34
|
$clone->add($salt); |
|
284
|
|
|
|
|
|
|
|
|
285
|
10
|
|
|
|
|
66
|
my $gen = &MIME::Base64::encode_base64( $clone->digest . $salt, '' ); |
|
286
|
10
|
|
|
|
|
22
|
my $scheme = $self->{scheme}; |
|
287
|
|
|
|
|
|
|
|
|
288
|
10
|
|
|
|
|
63
|
return "{$scheme}$gen"; |
|
289
|
|
|
|
|
|
|
} |
|
290
|
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=item B |
|
292
|
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
Validates a hasheddata previously generated against cleardata. I<$salt_len> defaults to 4 if not set. |
|
294
|
|
|
|
|
|
|
Returns 1 if the validation is successful, 0 otherwise. |
|
295
|
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
=cut |
|
297
|
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
sub validate { |
|
299
|
4
|
|
|
4
|
1
|
236333
|
my ( undef, $hasheddata, $cleardata, $salt_len ) = @_; |
|
300
|
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
# trim white-spaces |
|
302
|
4
|
|
|
|
|
14
|
$hasheddata =~ s!^\s+!!; |
|
303
|
4
|
|
|
|
|
16
|
$hasheddata =~ s!\s+$!!; |
|
304
|
|
|
|
|
|
|
|
|
305
|
4
|
|
|
|
|
12
|
my $scheme = &__get_pass_scheme($hasheddata); |
|
306
|
4
|
100
|
|
|
|
28
|
$scheme = uc( $scheme ) if $scheme; |
|
307
|
4
|
|
|
|
|
11
|
my $algorithm = &__make_algorithm($scheme); |
|
308
|
4
|
|
100
|
|
|
12
|
my $hash = &__get_pass_hash($hasheddata) || ''; |
|
309
|
4
|
|
|
|
|
14
|
my $salt = &__extract_salt( $hash, $salt_len ); |
|
310
|
|
|
|
|
|
|
|
|
311
|
4
|
|
|
|
|
16
|
my $obj = __PACKAGE__->new( |
|
312
|
|
|
|
|
|
|
algorithm => $algorithm, |
|
313
|
|
|
|
|
|
|
salt => $salt, |
|
314
|
|
|
|
|
|
|
salt_len => $salt_len |
|
315
|
|
|
|
|
|
|
); |
|
316
|
4
|
|
|
|
|
14
|
$obj->add($cleardata); |
|
317
|
|
|
|
|
|
|
|
|
318
|
4
|
|
|
|
|
10
|
my $gen_hasheddata = $obj->generate; |
|
319
|
4
|
|
|
|
|
30
|
my $gen_hash = &__get_pass_hash($gen_hasheddata); |
|
320
|
|
|
|
|
|
|
|
|
321
|
4
|
|
|
|
|
14
|
return _secure_compare( $gen_hash, $hash ); |
|
322
|
|
|
|
|
|
|
} |
|
323
|
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
=item B |
|
325
|
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
Returns a handle to L object. |
|
327
|
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
=cut |
|
329
|
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
sub obj { |
|
331
|
20
|
|
|
20
|
1
|
97
|
return shift->{digest}; |
|
332
|
|
|
|
|
|
|
} |
|
333
|
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
=back |
|
335
|
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
337
|
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
I |
|
339
|
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
=cut |
|
341
|
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
sub __make_scheme { |
|
343
|
|
|
|
|
|
|
|
|
344
|
9
|
|
|
9
|
|
18
|
my $scheme = shift; |
|
345
|
|
|
|
|
|
|
|
|
346
|
9
|
|
|
|
|
28
|
my @parts = split /-/, $scheme; |
|
347
|
9
|
100
|
|
|
|
25
|
pop @parts if $parts[-1] eq '1'; # SHA-1 => SHA |
|
348
|
|
|
|
|
|
|
|
|
349
|
9
|
|
|
|
|
25
|
$scheme = join '', @parts; |
|
350
|
|
|
|
|
|
|
|
|
351
|
9
|
|
|
|
|
52
|
return uc("S$scheme"); |
|
352
|
|
|
|
|
|
|
} |
|
353
|
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
sub __make_algorithm { |
|
355
|
4
|
|
|
4
|
|
10
|
my ( $algorithm ) = @_; |
|
356
|
|
|
|
|
|
|
|
|
357
|
4
|
|
100
|
|
|
13
|
$algorithm ||= ''; |
|
358
|
4
|
|
|
|
|
7
|
local $1; |
|
359
|
|
|
|
|
|
|
|
|
360
|
4
|
100
|
|
|
|
19
|
if ( $algorithm =~ m!^S(.*)$! ) { |
|
361
|
3
|
|
|
|
|
8
|
$algorithm = $1; |
|
362
|
|
|
|
|
|
|
|
|
363
|
|
|
|
|
|
|
# print STDERR "algorithm: $algorithm\n"; |
|
364
|
3
|
50
|
|
|
|
19
|
if ( $algorithm =~ m!([a-zA-Z]+)([0-9]+)! ) { |
|
365
|
|
|
|
|
|
|
|
|
366
|
3
|
|
|
|
|
8
|
my $name = uc($1); |
|
367
|
3
|
|
|
|
|
7
|
my $digits = $2; |
|
368
|
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
# print STDERR "name: $name\n"; |
|
370
|
|
|
|
|
|
|
# print STDERR "digits: $digits\n"; |
|
371
|
|
|
|
|
|
|
|
|
372
|
3
|
50
|
|
|
|
8
|
$name = "HMAC-$2" if $name =~ m!^HMAC(.*)$!; # HMAC-SHA-1 |
|
373
|
3
|
50
|
|
|
|
14
|
$digits = "-$digits" unless $name =~ m!MD$!; # MD2, MD4, MD5 |
|
374
|
|
|
|
|
|
|
|
|
375
|
3
|
|
|
|
|
8
|
$algorithm = "$name$digits"; |
|
376
|
|
|
|
|
|
|
} |
|
377
|
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
} |
|
379
|
|
|
|
|
|
|
|
|
380
|
4
|
|
|
|
|
12
|
return $algorithm; |
|
381
|
|
|
|
|
|
|
} |
|
382
|
|
|
|
|
|
|
|
|
383
|
|
|
|
|
|
|
sub __get_pass_scheme { |
|
384
|
4
|
|
|
4
|
|
11
|
local $1; |
|
385
|
4
|
100
|
|
|
|
23
|
return unless $_[0] =~ m/{([^}]*)/; |
|
386
|
3
|
|
|
|
|
13
|
return $1; |
|
387
|
|
|
|
|
|
|
} |
|
388
|
|
|
|
|
|
|
|
|
389
|
|
|
|
|
|
|
sub __get_pass_hash { |
|
390
|
8
|
|
|
8
|
|
19
|
local $1; |
|
391
|
8
|
100
|
|
|
|
39
|
return unless $_[0] =~ m/}(.*)/; |
|
392
|
7
|
|
|
|
|
24
|
return $1; |
|
393
|
|
|
|
|
|
|
} |
|
394
|
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
sub __generate_hex_salt { |
|
396
|
|
|
|
|
|
|
|
|
397
|
4
|
|
50
|
4
|
|
12
|
my $length = shift || 8; |
|
398
|
|
|
|
|
|
|
|
|
399
|
4
|
|
|
|
|
73
|
my $salt = substr( unpack( "h*", Crypt::SysRandom::random_bytes( POSIX::ceil( $length / 2 ) ) ), 0, $length ); |
|
400
|
|
|
|
|
|
|
|
|
401
|
4
|
|
|
|
|
23
|
return "HEX{$salt}"; |
|
402
|
|
|
|
|
|
|
} |
|
403
|
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
sub __extract_salt { |
|
405
|
|
|
|
|
|
|
|
|
406
|
4
|
|
|
4
|
|
9
|
my ( $hash, $salt_len ) = @_; |
|
407
|
|
|
|
|
|
|
|
|
408
|
4
|
|
|
|
|
15
|
my $binhash = &MIME::Base64::decode_base64($hash); |
|
409
|
4
|
|
100
|
|
|
23
|
my $binsalt = substr( $binhash, length($binhash) - ( $salt_len || 4 ) ); |
|
410
|
|
|
|
|
|
|
|
|
411
|
4
|
|
|
|
|
9
|
return $binsalt; |
|
412
|
|
|
|
|
|
|
} |
|
413
|
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
sub _secure_compare { |
|
415
|
4
|
|
|
4
|
|
10
|
my ($left, $right) = @_; |
|
416
|
4
|
|
|
|
|
13
|
my $res = length $left != length $right; |
|
417
|
4
|
100
|
|
|
|
11
|
$right = $left if $res; |
|
418
|
4
|
|
|
|
|
101
|
$res |= ord(substr $left, $_, 1) ^ ord(substr $right, $_, 1) for 0 .. length($left) - 1; |
|
419
|
4
|
|
|
|
|
46
|
return $res == 0; |
|
420
|
|
|
|
|
|
|
} |
|
421
|
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
423
|
|
|
|
|
|
|
|
|
424
|
|
|
|
|
|
|
L, L |
|
425
|
|
|
|
|
|
|
|
|
426
|
|
|
|
|
|
|
=head1 AUTHOR |
|
427
|
|
|
|
|
|
|
|
|
428
|
|
|
|
|
|
|
Sascha Kiefer, |
|
429
|
|
|
|
|
|
|
|
|
430
|
|
|
|
|
|
|
The current maintainer is Robert Rothenberg |
|
431
|
|
|
|
|
|
|
|
|
432
|
|
|
|
|
|
|
=head1 ACKNOWLEDGMENTS |
|
433
|
|
|
|
|
|
|
|
|
434
|
|
|
|
|
|
|
The author is particularly grateful to Andres Andreu for his article: Salted |
|
435
|
|
|
|
|
|
|
hashes demystified - A Primer (L) |
|
436
|
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
438
|
|
|
|
|
|
|
|
|
439
|
|
|
|
|
|
|
Copyright (C) 2005-2006, 2010, 2013, 2026 Sascha Kiefer |
|
440
|
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
|
442
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
|
443
|
|
|
|
|
|
|
|
|
444
|
|
|
|
|
|
|
=cut |
|
445
|
|
|
|
|
|
|
|
|
446
|
|
|
|
|
|
|
1; |