use strict;
use Carp;
+use Digest::SHA ();
+use Encode ();
+use PBKDF2::Tiny ();
-sub hash {
+sub hash_pkkdf2 {
my ($class, %params) = @_;
- if (!$params{algorithm}) {
- $params{algorithm} = 'SHA256S';
- $params{fallback_algorithm} = 'MD5';
- }
+ # PBKDF2::Tiny expects data to be in octets. Therefore we must
+ # encode everything we hand over (login, password) to UTF-8.
+ # This hash method uses a random hash and not just the user's login
+ # for its salt. This is due to the official recommendation that at
+ # least eight octets of random data should be used. Therefore we
+ # must store the salt together with the hashed password. The format
+ # in the database is:
- my $salt = $params{algorithm} =~ m/S$/ ? $params{login} : '';
+ # {PBKDF2}salt-in-hex:hash-in-hex
- if ($params{algorithm} =~ m/^SHA256/) {
- if (eval { require Digest::SHA; 1 }) {
- return '{' . $params{algorithm} . '}' . Digest::SHA::sha256_hex($salt . $params{password});
+ my $salt;
- } elsif ($params{fallback_algorithm}) {
- return $class->hash(%params, algorithm => $params{fallback_algorithm});
+ if ((defined $params{stored_password}) && ($params{stored_password} =~ m/^\{PBKDF2\} ([0-9a-f]+) :/x)) {
+ $salt = (split m{:}, Encode::encode('utf-8', $1), 2)[0];
- } else {
- die 'Digest::SHA is not available';
- }
+ } else {
+ my @login = map { ord } split m{}, Encode::encode('utf-8', $params{login});
+ my @random = map { int(rand(256)) } (0..16);
- } elsif ($params{algorithm} =~ m/^SHA1/) {
- if (eval { require Digest::SHA; 1 }) {
- return '{' . $params{algorithm} . '}' . Digest::SHA::sha1_hex($salt . $params{password});
+ $salt = join '', map { sprintf '%02x', $_ } @login, @random;
+ }
- } elsif (eval { require Digest::SHA1; 1 }) {
- return '{' . $params{algorithm} . '}' . Digest::SHA1::sha1_hex($salt . $params{password});
+ my $hashed = "{PBKDF2}${salt}:" . join('', map { sprintf '%02x', ord } split m{}, PBKDF2::Tiny::derive('SHA-256', $salt, Encode::encode('utf-8', $params{password})));
- } elsif ($params{fallback_algorithm}) {
- return $class->hash(%params, algorithm => $params{fallback_algorithm});
+ return $hashed;
+}
- } else {
- die 'Neither Digest::SHA nor Digest::SHA1 is available';
- }
+sub hash {
+ my ($class, %params) = @_;
+
+ $params{algorithm} ||= 'PBKDF2';
- } elsif ($params{algorithm} =~ m/^MD5/) {
- require Digest::MD5;
- return '{' . $params{algorithm} . '}' . Digest::MD5::md5_hex($salt . $params{password});
+ my $salt = $params{algorithm} =~ m/S$/ ? $params{login} : '';
+
+ if ($params{algorithm} =~ m/^SHA256/) {
+ return '{' . $params{algorithm} . '}' . Digest::SHA::sha256_hex($salt . $params{password});
- } elsif ($params{algorithm} eq 'CRYPT') {
- return '{CRYPT}' . crypt($params{password}, substr($params{login}, 0, 2));
+ } elsif ($params{algorithm} =~ m/^PBKDF2/) {
+ return $class->hash_pkkdf2(password => $params{password}, stored_password => $params{stored_password});
} else {
croak 'Unsupported hash algorithm ' . $params{algorithm};
my ($class, $password, $default_algorithm) = @_;
return ($1, $2) if $password =~ m/^\{ ([^\}]+) \} (.+)/x;
- return ($default_algorithm || 'CRYPT', $password);
+ return ($default_algorithm || 'PBKDF2', $password);
}
1;