Crypt-SaltedHash
view release on metacpan or search on metacpan
lib/Crypt/SaltedHash.pm view on Meta::CPAN
In pseudocode we generate a salted hash as follows:
Get the source string and salt as separate binary objects
Concatenate the 2 binary values
Hash the concatenation into SaltedPasswordHash
Base64Encode(concat(SaltedPasswordHash, Salt))
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.
Then we have the Salt value, which are typically 4 bytes of purely random binary data represented as hexadecimal notation (Base16 as 8 bytes).
Using SHA-1 as the hashing algorithm, SaltedPasswordHash is of length 20 (bytes) in raw binary form
(40 bytes if we look at it in hex). Salt is then 4 bytes in raw binary form. The SHA-1 algorithm generates
a 160 bit hash string. Consider that 8 bits = 1 byte. So 160 bits = 20 bytes, which is exactly what the
algorithm gives us.
The Base64 encoding of the binary result looks like:
{SSHA}B0O0XSYdsk7g9K229ZEr73Lid7HBD9DX
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.
A couple of examples of salted hashes using on the same exact clear-text string:
slappasswd -s testing123
{SSHA}72uhy5xc1AWOLwmNcXALHBSzp8xt4giL
slappasswd -s testing123
{SSHA}zmIAVaKMmTngrUi4UlS0dzYwVAbfBTl7
slappasswd -s testing123
{SSHA}Be3F12VVvBf9Sy6MSqpOgAdEj6JCZ+0f
slappasswd -s testing123
{SSHA}ncHs4XYmQKJqL+VuyNQzQjwRXfvu6noa
4 runs of slappasswd against the same clear text string each yielded unique endresult hashes.
The random salt is generated silently and never made visible.
=head2 Extracting the data
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
given clear text string, and then it is stored within the final output as purely Base64 encoded data. In order to perform an authentication
query for instance, we must break apart the concatenation that was created for storage of the data. We accomplish this by splitting
up the binary data we get after Base64 decoding the stored hash.
In pseudocode we would perform the extraction and verification operations as such:
Strip the hash identifier from the Digest
Base64Decode(Digest, 20)
Split Digest into 2 byte arrays, one for bytes 0 � 20(pwhash), one for bytes 21 � 32 (salt)
Get the target string and salt as separate binary object
Concatenate the 2 binary values
SHA hash the concatenation into targetPasswordHash
Compare targetPasswordHash with pwhash
Return corresponding Boolean value
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
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
match against the new clear text presented for verification. The string presented for verification will have to be salted as well. The rest
of the bytes (21 � 32) represent the random salt which when decoded will show the exact hex characters that make up the once randomly
generated seed.
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
random salt and then using that verify the clear text string hashed by slappasswd. First, let's do a verification test with an erroneous
password; this should fail the matching test:
{SSHA}72uhy5xc1AWOLwmNcXALHBSzp8xt4giL Test123
Hash extracted (in hex): ef6ba1cb9c5cd4058e2f098d71700b1c14b3a7cc
Salt extracted (in hex): 6de2088b
Hash length is: 20 Salt length is: 4
Hash presented in hex: 256bc48def0ce04b0af90dfd2808c42588bf9542
Hashes DON'T match: Test123
The match failure test was successful as expected. Now let's use known valid data through the same exact code:
{SSHA}72uhy5xc1AWOLwmNcXALHBSzp8xt4giL testing123
Hash extracted (in hex): ef6ba1cb9c5cd4058e2f098d71700b1c14b3a7cc
Salt extracted (in hex): 6de2088b
Hash length is: 20 Salt length is: 4
Hash presented in hex: ef6ba1cb9c5cd4058e2f098d71700b1c14b3a7cc
Hashes match: testing123
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
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.
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
for various purposes. Obviously the usage can be on either side of the colored hat line, but the data is there.
=head1 METHODS
=over 4
=item B<new( [%options] )>
Returns a new Crypt::SaltedHash object.
Possible keys for I<%options> are:
=over
=item *
I<algorithm>: It's also possible to use common string representations of the
algorithm (e.g. "sha256", "SHA-384"). If the argument is missing, SHA-1 will
be used by default.
=item *
I<salt>: You can specify your on salt. You can either specify it as a sequence
of characters or as a hex encoded string of the form "HEX{...}". If the argument is missing,
a random seed is provided for you (recommended).
=item *
I<salt_len>: By default, the module assumes a salt length of 4 bytes (or 8, if it is encoded in hex).
If you choose a different length, you have to tell the I<validate> function how long your seed was.
=back
=cut
sub new {
( run in 0.818 second using v1.01-cache-2.11-cpan-2398b32b56e )