X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FAuth%2FPassword.pm;h=ec6fc72ecf8bc19b13ceeacafb7785a865c6ee6a;hb=496f2f9f8574affb940c5b83deb837860a3b112a;hp=9b0f1aec7be4ccfa8e5878eb4a88bbe406655929;hpb=58fdd50dbb6b909d48b2846f36857b2bd2219441;p=kivitendo-erp.git diff --git a/SL/Auth/Password.pm b/SL/Auth/Password.pm index 9b0f1aec7..ec6fc72ec 100644 --- a/SL/Auth/Password.pm +++ b/SL/Auth/Password.pm @@ -8,24 +8,41 @@ sub hash { my ($class, %params) = @_; if (!$params{algorithm}) { - $params{algorithm} = 'SHA1'; + $params{algorithm} = 'SHA256S'; $params{fallback_algorithm} = 'MD5'; } - if ($params{algorithm} eq 'SHA1') { - if (eval { require Digest::SHA1; 1 }) { - return '{SHA1}' . Digest::SHA1::sha1_hex($params{password}); + + my $salt = $params{algorithm} =~ m/S$/ ? $params{login} : ''; + + if ($params{algorithm} =~ m/^SHA256/) { + if (eval { require Digest::SHA; 1 }) { + return '{' . $params{algorithm} . '}' . Digest::SHA::sha256_hex($salt . $params{password}); + + } elsif ($params{fallback_algorithm}) { + return $class->hash(%params, algorithm => $params{fallback_algorithm}); + + } else { + die 'Digest::SHA is not available'; + } + + } elsif ($params{algorithm} =~ m/^SHA1/) { + if (eval { require Digest::SHA; 1 }) { + return '{' . $params{algorithm} . '}' . Digest::SHA::sha1_hex($salt . $params{password}); + + } elsif (eval { require Digest::SHA1; 1 }) { + return '{' . $params{algorithm} . '}' . Digest::SHA1::sha1_hex($salt . $params{password}); } elsif ($params{fallback_algorithm}) { - return $class->hash_password(%params, algorithm => $params{fallback_algorithm}); + return $class->hash(%params, algorithm => $params{fallback_algorithm}); } else { - die 'Digest::SHA1 not available'; + die 'Neither Digest::SHA nor Digest::SHA1 is available'; } - } elsif ($params{algorithm} eq 'MD5') { + } elsif ($params{algorithm} =~ m/^MD5/) { require Digest::MD5; - return '{MD5}' . Digest::MD5::md5_hex($params{password}); + return '{' . $params{algorithm} . '}' . Digest::MD5::md5_hex($salt . $params{password}); } elsif ($params{algorithm} eq 'CRYPT') { return '{CRYPT}' . crypt($params{password}, substr($params{login}, 0, 2)); @@ -35,11 +52,27 @@ sub hash { } } +sub hash_if_unhashed { + my ($class, %params) = @_; + + my ($algorithm, $password) = $class->parse($params{password}, 'NONE'); + + 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); +} + sub parse { - my ($class, $password) = @_; + my ($class, $password, $default_algorithm) = @_; return ($1, $2) if $password =~ m/^\{ ([^\}]+) \} (.+)/x; - return ('CRYPT', $password); + return ($default_algorithm || 'CRYPT', $password); } 1;