Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 58fdd50d

Von Moritz Bunkus vor mehr als 13 Jahren hinzugefügt

  • ID 58fdd50dbb6b909d48b2846f36857b2bd2219441
  • Vorgänger 1d69ac97
  • Nachfolger d0c2cfbe

Passwort-Hashing in eigenes Modul ausgelagert

Unterschiede anzeigen:

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;
SL/Auth/Password.pm
1
package SL::Auth::Password;
2

  
3
use strict;
4

  
5
use Carp;
6

  
7
sub hash {
8
  my ($class, %params) = @_;
9

  
10
  if (!$params{algorithm}) {
11
    $params{algorithm}          = 'SHA1';
12
    $params{fallback_algorithm} = 'MD5';
13
  }
14

  
15
  if ($params{algorithm} eq 'SHA1') {
16
    if (eval { require Digest::SHA1; 1 }) {
17
      return '{SHA1}' . Digest::SHA1::sha1_hex($params{password});
18

  
19
    } elsif ($params{fallback_algorithm}) {
20
      return $class->hash_password(%params, algorithm => $params{fallback_algorithm});
21

  
22
    } else {
23
      die 'Digest::SHA1 not available';
24
    }
25

  
26
  } elsif ($params{algorithm} eq 'MD5') {
27
    require Digest::MD5;
28
    return '{MD5}' . Digest::MD5::md5_hex($params{password});
29

  
30
  } elsif ($params{algorithm} eq 'CRYPT') {
31
    return '{CRYPT}' . crypt($params{password}, substr($params{login}, 0, 2));
32

  
33
  } else {
34
    croak 'Unsupported hash algorithm ' . $params{algorithm};
35
  }
36
}
37

  
38
sub parse {
39
  my ($class, $password) = @_;
40

  
41
  return ($1, $2) if $password =~ m/^\{ ([^\}]+) \} (.+)/x;
42
  return ('CRYPT', $password);
43
}
44

  
45
1;

Auch abrufbar als: Unified diff