kivitendo/SL/Auth/Password.pm @ a9107c90
58fdd50d | Moritz Bunkus | package SL::Auth::Password;
|
||
use strict;
|
||||
use Carp;
|
||||
9d4c2b6c | Moritz Bunkus | use Digest::SHA ();
|
||
c157c911 | Moritz Bunkus | use Encode ();
|
||
use PBKDF2::Tiny ();
|
||||
sub hash_pkkdf2 {
|
||||
my ($class, %params) = @_;
|
||||
# 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:
|
||||
# {PBKDF2}salt-in-hex:hash-in-hex
|
||||
my $salt;
|
||||
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 {
|
||||
my @login = map { ord } split m{}, Encode::encode('utf-8', $params{login});
|
||||
my @random = map { int(rand(256)) } (0..16);
|
||||
$salt = join '', map { sprintf '%02x', $_ } @login, @random;
|
||||
}
|
||||
my $hashed = "{PBKDF2}${salt}:" . join('', map { sprintf '%02x', ord } split m{}, PBKDF2::Tiny::derive('SHA-256', $salt, Encode::encode('utf-8', $params{password})));
|
||||
return $hashed;
|
||||
}
|
||||
58fdd50d | Moritz Bunkus | |||
sub hash {
|
||||
my ($class, %params) = @_;
|
||||
c157c911 | Moritz Bunkus | $params{algorithm} ||= 'PBKDF2';
|
||
e0c3dcb8 | Moritz Bunkus | |||
my $salt = $params{algorithm} =~ m/S$/ ? $params{login} : '';
|
||||
if ($params{algorithm} =~ m/^SHA256/) {
|
||||
9d4c2b6c | Moritz Bunkus | return '{' . $params{algorithm} . '}' . Digest::SHA::sha256_hex($salt . $params{password});
|
||
e0c3dcb8 | Moritz Bunkus | |||
c157c911 | Moritz Bunkus | } elsif ($params{algorithm} =~ m/^PBKDF2/) {
|
||
return $class->hash_pkkdf2(password => $params{password}, stored_password => $params{stored_password});
|
||||
58fdd50d | Moritz Bunkus | } else {
|
||
croak 'Unsupported hash algorithm ' . $params{algorithm};
|
||||
}
|
||||
}
|
||||
d0c2cfbe | Moritz Bunkus | sub hash_if_unhashed {
|
||
my ($class, %params) = @_;
|
||||
my ($algorithm, $password) = $class->parse($params{password}, 'NONE');
|
||||
d3d6cb31 | Moritz Bunkus | return $params{password} unless $algorithm eq 'NONE';
|
||
if ($params{look_up_algorithm}) {
|
||||
my $stored_password = $params{auth}->get_stored_password($params{login});
|
||||
my ($stored_algorithm) = $class->parse($stored_password);
|
||||
$params{algorithm} = $stored_algorithm;
|
||||
}
|
||||
return $class->hash(%params);
|
||||
d0c2cfbe | Moritz Bunkus | }
|
||
58fdd50d | Moritz Bunkus | sub parse {
|
||
d0c2cfbe | Moritz Bunkus | my ($class, $password, $default_algorithm) = @_;
|
||
58fdd50d | Moritz Bunkus | |||
return ($1, $2) if $password =~ m/^\{ ([^\}]+) \} (.+)/x;
|
||||
69af5044 | Moritz Bunkus | return ($default_algorithm || 'PBKDF2', $password);
|
||
58fdd50d | Moritz Bunkus | }
|
||
1;
|