- if (!$params{algorithm}) {
- $params{algorithm} = 'SHA1';
- $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:
+
+ # {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;