Authen-Passphrase-SaltedSHA512
view release on metacpan or search on metacpan
lib/Authen/Passphrase/SaltedSHA512.pm view on Meta::CPAN
my $challenge = Authen::Passphrase::SaltedSHA512->new(
salt_hex => $salt,
hash_hex => $hash
);
# And challenge the passphrase supplied for the current session's login.
if( $challenge->match( 'Sneaky!' ) ) {
print "You are a winner!\n";
}
# Or for the ultimate in ease and simplicity:
use Authen::Passphrase::SaltedSHA512 qw(
generate_salted_sha512 validate_salted_sha512
);
my ( $salt_hex, $hash_hex ) = generate_salted_sha512( $passphrase );
my $is_valid = validate_salted_sha512( $passphrase, $salt_hex, $hash_hex );
=head1 DESCRIPTION
Authen::Passhprase::SaltedSHA512 is designed to simplify the process of
generating random salt for, and a salted hash of a user supplied passphrase.
It is also designed to easily authenticate a user supplied passphrase against
a given salt and hash.
The presumed use-case is for user authentication where a salt and a password
hash will be stored in a database of user logins. The simple interface should
fit into a broad range of authentication systems with minimal clutter.
Authen::Passphrase::SaltedSHA512 is a subclass of
L<Authen::Passphrase::SaltedDigest|http://search.cpan.org/perldoc?Authen::Passphrase::SaltedDigest>
that overrides the constructor to provide reasonable defaults so that you
don't have to spend a week reading articles on which algorithm to use, and how
to generate a good salt.
The hashing algorithm chosen is the SHA-512 hash function from the SHA-2
family. Currently SHA-512 is a leading edge standard in strong hashing.
The salt generated when creating authentication credentials is a 512 bit
random string. The random number generating algorithm used comes from
L<Bytes::Random::Secure|http://search.cpan.org/perldoc?Bytes::Random::Secure>.
That module uses Math::Random::ISAAC, "I<...a cryptographically-strong random
number generator with no known serious weaknesses.>" Bytes::Random::Secure
obtains its seed using Crypt::Random::Seed. The reason that
Bytes::Random::Secure was chosen over other random number generators is because
that module has a light-weight dependency chain, a cryptographically strong
random number generator, strong seeding (the hardest part of the CSPRNG problem)
across a wide variety of platforms, and useful hex output.
By using a 512 bit random salt, a maximum degree of entropy is achieved in the
hashes generated by the SHA-512 algorithm. Every time the constructor is
called you will get a new random salt, so every user has his own salt. The
advantage of using a fresh random salt for each user is that it eliminates the
rainbow table attack vector, by guaranteeing that if one user's password is
compromised through brute force (or cosmic good luck) all of your other users
with their own random salts are still secure.
By selecting secure defaults for hashing algorithm, random number generation,
and salt bit-length, much of the guesswork can be eliminated from devising
an authentication scheme, and a simpler user interface results.
=head1 EXPORT
This is primarily an Object Oriented Interface module. However, for even
greater simplicity, a standard functions interface is provided upon request.
Nothing is exported by default. By supplying an export list, the following
subroutines are available:
=over 4
=item * generate_salted_sha512
=item * validate_salted_sha512
=back
=head1 SUBROUTINES/METHODS
=head2 METHODS
The following section describes the methods available through the module's
Object Oriented interface.
=head3 new
B<The constructor> will create an object that can either be used to generate
a salt and a hash for later use, or to challenge a supplied salt and hash
by a passphrase supplied to C<match>.
Instantiate a salt and hash generator object.
my $auth_gen = Authen::Passphrase::SaltedSHA512->new(
passphrase => 'All your base are belong to us.'
);
Instantiate a challenge object.
my $challenger = Authen::Passphrase::SaltedSHA512->new(
salt_hex => $retrieved_salt,
hash_hex => $retrieved_hash
);
=head4 Constructor Parameters
For passphrase hash and salt generation, you must supply the C<passphrase>
parameter. For validation, you must supply either a raw or a hex salt, using
the C<salt> or C<salt_hex> parameters, and either a raw or a hex hash, using
the C<hash> or C<hash_hex> parameters. These are described below.
=over
=item B<salt>
The salt, as a raw string of bytes. Defaults to the empty string,
yielding an unsalted scheme.
lib/Authen/Passphrase/SaltedSHA512.pm view on Meta::CPAN
The salt, as a string of hexadecimal digits. Defaults to the empty
string, yielding an unsalted scheme.
=item B<hash>
The hash, as a string of bytes.
=item B<hash_hex>
The hash, as a string of hexadecimal digits.
=item B<passphrase>
A passphrase that will be accepted.
=back
You must supply either a C<passphrase>, or a C<salt> and a C<hash>. The
C<salt> and the C<hash> may either be supplied as C<< salt => $raw_string >>
and C<< hash => $raw_string >> or as C<< salt_hex => $hex_digits >> and
C<< hash_hex => $hex_digits >>. Both the salt and the hash will be 512 bits
long, or 128 hex digits.
If a C<passphrase> is suppled, a generator object will be created. If some
form of C<salt> and C<hash> are supplied, a challenge object will be created.
=head3 salt
Returns the 512 bit salt, in raw form (64 bytes).
my $salt = $auth_gen->salt;
=head3 salt_hex
Returns the salt, as a string of 128 hexidecimal digits.
my $salt_hash = $auth_gen->salt_hash;
=head3 hash
Returns the 512 bit hash, in raw form.
my $hash = $auth_gen->hash;
=head3 hash_hex
Returns the hash, as a string of 128 hexidecimal digits.
my $hash_hex = $auth_gen->hash;
=head3 match
Returns true if C<$passphrase> matches against the salt and hash supplied to
the constructor, and false otherwise.
if( $challenge->match( $passphrase ) ) {
print "Your passphrase has been authenticated.\n";
}
else {
print "Invalid passphrase.\n",
"You have 1.34e154 more tries before exhausing all possible guesses.\n",
"Happy hunting!\n";
}
=head3 algorithm
Returns the digest algorithm, which will always be C<SHA-512>.
=head2 SUBROUTINES
This section describes the subroutines used in this module's
standard (non-OO) interface.
=head3 generate_salted_sha512
Accepts a C<$passphrase> parameter, and returns a list containing a hex
representation of a random salt, and of the hash.
my( $salt_hex, $hash_hex ) = generate_salted_sha512( 'Groovy Password' );
=head3 validate_salted_sha512
Accepts parameters of C<$passphrase>, C<$salt_hex>, and C<$hash_hex>, and
returns true if C<$passphrase> authenticates against the given salt and hash.
my $is_valid = validate_salted_sha512(
'Groovy Password',
$salt_hex,
$hash_hex
);
=head1 IMPLEMENTATION DETAILS
While the choice of the SHA-2 SHA-512 function was an easy one, selecting a
random number generator for building cryptographically useful salt proved more
difficult. Some modules are using one of the Math::Random::MT modules, yet
the POD for Math::Random::MT states, "This algorithm has a very uniform
distribution and is good for modelling purposes but do not use it for
cryptography."
Authen::Passphrase::SaltedDigest could generate random salt, but relies on
Data::Entropy to do so. Data::Entropy::Algorithm, by default seems to be
constrained by the quality of Perl's C<seed>, which is probably not as secure
of a source as should be used. That leads to a search for a better solution.
The list of other possibilities is long, and while many of them might turn out
to be reasonable choices,
L<Math::Random::Secure|http://search.cpan.org/perldoc?Math::Random::Secure>
seemed to offer a solution that is secure today, and should continue to follow
I<Best Practices> as new trends emerge. The disadvantage is that it is heavy
on dependencies. This seems to be the price one has to pay for a really good
random source.
=head1 COMPATIBILITY
Because Authen::Passphrase::SaltedSHA512 is a subclass of
L<Authen::Passphrase::SaltedDigest|http://search.cpan.org/perldoc?Authen::Passphrase::SaltedDigest>,
the hash and salt generated can also be challenged using
( run in 0.652 second using v1.01-cache-2.11-cpan-364913b4093 )