1 package SL::Auth::Password;
10 my ($class, %params) = @_;
12 $params{algorithm} ||= 'SHA256S';
14 my $salt = $params{algorithm} =~ m/S$/ ? $params{login} : '';
16 if ($params{algorithm} =~ m/^SHA256/) {
17 return '{' . $params{algorithm} . '}' . Digest::SHA::sha256_hex($salt . $params{password});
19 } elsif ($params{algorithm} =~ m/^SHA1/) {
20 return '{' . $params{algorithm} . '}' . Digest::SHA::sha1_hex($salt . $params{password});
22 } elsif ($params{algorithm} =~ m/^MD5/) {
23 return '{' . $params{algorithm} . '}' . Digest::MD5::md5_hex($salt . $params{password});
25 } elsif ($params{algorithm} eq 'CRYPT') {
26 return '{CRYPT}' . crypt($params{password}, substr($params{login}, 0, 2));
29 croak 'Unsupported hash algorithm ' . $params{algorithm};
33 sub hash_if_unhashed {
34 my ($class, %params) = @_;
36 my ($algorithm, $password) = $class->parse($params{password}, 'NONE');
38 return $params{password} unless $algorithm eq 'NONE';
40 if ($params{look_up_algorithm}) {
41 my $stored_password = $params{auth}->get_stored_password($params{login});
42 my ($stored_algorithm) = $class->parse($stored_password);
43 $params{algorithm} = $stored_algorithm;
46 return $class->hash(%params);
50 my ($class, $password, $default_algorithm) = @_;
52 return ($1, $2) if $password =~ m/^\{ ([^\}]+) \} (.+)/x;
53 return ($default_algorithm || 'CRYPT', $password);