d566cf0f19fce3f482c694ef3f23951e3c0c658d
[kivitendo-erp.git] / SL / Auth / Password.pm
1 package SL::Auth::Password;
2
3 use strict;
4
5 use Carp;
6 use Digest::MD5 ();
7 use Digest::SHA ();
8 use Encode ();
9 use PBKDF2::Tiny ();
10
11 sub hash_pkkdf2 {
12   my ($class, %params) = @_;
13
14   # PBKDF2::Tiny expects data to be in octets. Therefore we must
15   # encode everything we hand over (login, password) to UTF-8.
16
17   # This hash method uses a random hash and not just the user's login
18   # for its salt. This is due to the official recommendation that at
19   # least eight octets of random data should be used. Therefore we
20   # must store the salt together with the hashed password. The format
21   # in the database is:
22
23   # {PBKDF2}salt-in-hex:hash-in-hex
24
25   my $salt;
26
27   if ((defined $params{stored_password}) && ($params{stored_password} =~ m/^\{PBKDF2\} ([0-9a-f]+) :/x)) {
28     $salt = (split m{:}, Encode::encode('utf-8', $1), 2)[0];
29
30   } else {
31     my @login  = map { ord } split m{}, Encode::encode('utf-8', $params{login});
32     my @random = map { int(rand(256)) } (0..16);
33
34     $salt      = join '', map { sprintf '%02x', $_ } @login, @random;
35   }
36
37   my $hashed = "{PBKDF2}${salt}:" . join('', map { sprintf '%02x', ord } split m{}, PBKDF2::Tiny::derive('SHA-256', $salt, Encode::encode('utf-8', $params{password})));
38
39   return $hashed;
40 }
41
42 sub hash {
43   my ($class, %params) = @_;
44
45   $params{algorithm} ||= 'PBKDF2';
46
47   my $salt = $params{algorithm} =~ m/S$/ ? $params{login} : '';
48
49   if ($params{algorithm} =~ m/^SHA256/) {
50     return '{' . $params{algorithm} . '}' . Digest::SHA::sha256_hex($salt . $params{password});
51
52   } elsif ($params{algorithm} =~ m/^SHA1/) {
53     return '{' . $params{algorithm} . '}' . Digest::SHA::sha1_hex($salt . $params{password});
54
55   } elsif ($params{algorithm} =~ m/^MD5/) {
56     return '{' . $params{algorithm} . '}' . Digest::MD5::md5_hex($salt . $params{password});
57
58   } elsif ($params{algorithm} eq 'CRYPT') {
59     return '{CRYPT}' . crypt($params{password}, substr($params{login}, 0, 2));
60
61   } elsif ($params{algorithm} =~ m/^PBKDF2/) {
62     return $class->hash_pkkdf2(password => $params{password}, stored_password => $params{stored_password});
63
64   } else {
65     croak 'Unsupported hash algorithm ' . $params{algorithm};
66   }
67 }
68
69 sub hash_if_unhashed {
70   my ($class, %params) = @_;
71
72   my ($algorithm, $password) = $class->parse($params{password}, 'NONE');
73
74   return $params{password} unless $algorithm eq 'NONE';
75
76   if ($params{look_up_algorithm}) {
77     my $stored_password    = $params{auth}->get_stored_password($params{login});
78     my ($stored_algorithm) = $class->parse($stored_password);
79     $params{algorithm}     = $stored_algorithm;
80   }
81
82   return $class->hash(%params);
83 }
84
85 sub parse {
86   my ($class, $password, $default_algorithm) = @_;
87
88   return ($1, $2) if $password =~ m/^\{ ([^\}]+) \} (.+)/x;
89   return ($default_algorithm || 'CRYPT', $password);
90 }
91
92 1;