Revision 58fdd50d
Von Moritz Bunkus vor mehr als 13 Jahren hinzugefügt
SL/Auth/DB.pm | ||
---|---|---|
6 | 6 |
use Scalar::Util qw(weaken); |
7 | 7 |
|
8 | 8 |
use SL::Auth::Constants qw(:all); |
9 |
use SL::Auth::Password; |
|
9 | 10 |
use SL::DBUtils; |
10 | 11 |
|
11 | 12 |
sub new { |
... | ... | |
45 | 46 |
|
46 | 47 |
# Empty password hashes in the database mean just that -- empty |
47 | 48 |
# passwords. Hash it for easier comparison. |
48 |
$stored_password = $self->hash_password(password => $stored_password) unless $stored_password;
|
|
49 |
($algorithm, $stored_password) = $self->parse_password_entry($stored_password);
|
|
50 |
($algorithm2, $password) = $self->parse_password_entry($self->hash_password(password => $password, algorithm => $algorithm, login => $login));
|
|
49 |
$stored_password = SL::Auth::Password->hash(password => $stored_password) unless $stored_password;
|
|
50 |
($algorithm, $stored_password) = SL::Auth::Password->parse($stored_password);
|
|
51 |
($algorithm2, $password) = SL::Auth::Password->parse(SL::Auth::Password->hash(password => $password, algorithm => $algorithm, login => $login));
|
|
51 | 52 |
|
52 | 53 |
$main::lxdebug->leave_sub(); |
53 | 54 |
|
... | ... | |
73 | 74 |
return ERR_BACKEND; |
74 | 75 |
} |
75 | 76 |
|
76 |
$password = $self->hash_password(password => $password) unless $is_crypted;
|
|
77 |
$password = SL::Auth::Password->hash(password => $password) unless $is_crypted;
|
|
77 | 78 |
|
78 | 79 |
do_query($main::form, $dbh, qq|UPDATE auth."user" SET password = ? WHERE login = ?|, $password, $login); |
79 | 80 |
|
... | ... | |
88 | 89 |
return 1; |
89 | 90 |
} |
90 | 91 |
|
91 |
sub hash_password { |
|
92 |
my ($self, %params) = @_; |
|
93 |
|
|
94 |
if (!$params{algorithm}) { |
|
95 |
$params{algorithm} = 'SHA1'; |
|
96 |
$params{fallback_algorithm} = 'MD5'; |
|
97 |
} |
|
98 |
|
|
99 |
if ($params{algorithm} eq 'SHA1') { |
|
100 |
if (eval { require Digest::SHA1; 1 }) { |
|
101 |
return '{SHA1}' . Digest::SHA1::sha1_hex($params{password}); |
|
102 |
|
|
103 |
} elsif ($params{fallback_algorithm}) { |
|
104 |
return $self->hash_password(%params, algorithm => $params{fallback_algorithm}); |
|
105 |
|
|
106 |
} else { |
|
107 |
die 'Digest::SHA1 not available'; |
|
108 |
} |
|
109 |
|
|
110 |
} elsif ($params{algorithm} eq 'MD5') { |
|
111 |
require Digest::MD5; |
|
112 |
return '{MD5}' . Digest::MD5::md5_hex($params{password}); |
|
113 |
|
|
114 |
} elsif ($params{algorithm} eq 'CRYPT') { |
|
115 |
return '{CRYPT}' . crypt($params{password}, substr($params{login}, 0, 2)); |
|
116 |
|
|
117 |
} else { |
|
118 |
croak 'Unsupported hash algorithm ' . $params{algorithm}; |
|
119 |
} |
|
120 |
} |
|
121 |
|
|
122 |
sub parse_password_entry { |
|
123 |
my ($self, $password) = @_; |
|
124 |
|
|
125 |
return ($1, $2) if $password =~ m/^\{ ([^\}]+) \} (.+)/x; |
|
126 |
return ('CRYPT', $password); |
|
127 |
} |
|
128 |
|
|
129 | 92 |
1; |
Auch abrufbar als: Unified diff
Passwort-Hashing in eigenes Modul ausgelagert