|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Todd Burry <[email protected]> |
|
4
|
|
|
* @copyright 2009-2014 Vanilla Forums Inc. |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Garden\Password; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Implements the password hashing algorithm of punBB. |
|
13
|
|
|
* |
|
14
|
|
|
* In order to use this class with passwords from a punbb database concatenate the password hashes and salts. |
|
15
|
|
|
* |
|
16
|
|
|
* ```php |
|
17
|
|
|
* // php |
|
18
|
|
|
* $hash = $password.'$'.$salt; |
|
19
|
|
|
* ``` |
|
20
|
|
|
* |
|
21
|
|
|
* ```sql |
|
22
|
|
|
* -- mysql |
|
23
|
|
|
* select concat(u.password, '$', u.salt) as password_hash |
|
24
|
|
|
* from punbb_users u; |
|
25
|
|
|
* ``` |
|
26
|
|
|
*/ |
|
27
|
|
|
class PunbbPassword implements IPassword { |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
3 |
|
public function hash($password) { |
|
33
|
3 |
|
$salt = base64_encode(openssl_random_pseudo_bytes(12)); |
|
34
|
3 |
|
return $this->hashRaw($password, $salt).'$'.$salt; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Hashes a password with a given salt. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $password The password to hash. |
|
41
|
|
|
* @param string $salt The password salt. |
|
42
|
|
|
* @return string Returns the password hash. |
|
43
|
|
|
*/ |
|
44
|
3 |
|
protected function hashRaw($password, $salt) { |
|
45
|
3 |
|
$calc_hash = sha1($salt.sha1($password)); |
|
46
|
|
|
|
|
47
|
3 |
|
return $calc_hash; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
1 |
View Code Duplication |
public function needsRehash($hash) { |
|
54
|
1 |
|
list($stored_hash, $stored_salt) = $this->splitHash($hash); |
|
55
|
|
|
|
|
56
|
|
|
// Unsalted hashes should be rehashed. |
|
57
|
1 |
|
if ($stored_hash === false || $stored_salt === false) { |
|
58
|
|
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
3 |
|
public function verify($password, $hash) { |
|
68
|
3 |
|
list($stored_hash, $stored_salt) = $this->splitHash($hash); |
|
69
|
|
|
|
|
70
|
3 |
|
if (md5($password) == $stored_hash) { |
|
71
|
|
|
$result = true; |
|
72
|
3 |
|
} elseif (sha1($password) == $stored_hash) { |
|
73
|
|
|
$result = true; |
|
74
|
3 |
|
} elseif (sha1($stored_salt.sha1($password)) == $stored_hash) { |
|
75
|
1 |
|
$result = true; |
|
76
|
1 |
|
} else { |
|
77
|
2 |
|
$result = false; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
return $result; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Split the hash into its calculated hash and salt. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $hash The hash to split. |
|
87
|
|
|
* @return array An array in the form [$hash, $salt]. |
|
88
|
|
|
*/ |
|
89
|
4 |
|
protected function splitHash($hash) { |
|
90
|
4 |
|
if (strpos($hash, '$') === false) { |
|
91
|
1 |
|
return [$hash, false]; |
|
92
|
|
|
} else { |
|
93
|
3 |
|
$parts = explode('$', $hash, 2); |
|
94
|
3 |
|
return $parts; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|