|
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
|
|
|
* Implements the password hashing algorithm of Joomla. |
|
12
|
|
|
*/ |
|
13
|
|
View Code Duplication |
class JoomlaPassword implements IPassword { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* {@inheritdoc} |
|
17
|
|
|
*/ |
|
18
|
3 |
|
public function hash($password) { |
|
19
|
3 |
|
$salt = base64_encode(openssl_random_pseudo_bytes(12)); |
|
20
|
3 |
|
return $this->hashRaw($password, $salt).':'.$salt; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Hashes a password with a given salt. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $password The password to hash. |
|
27
|
|
|
* @param string $salt The password salt. |
|
28
|
|
|
* @return string Returns the password hash. |
|
29
|
|
|
*/ |
|
30
|
4 |
|
protected function hashRaw($password, $salt) { |
|
31
|
4 |
|
$calc_hash = md5($password.$salt); |
|
32
|
|
|
|
|
33
|
4 |
|
return $calc_hash; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function needsRehash($hash) { |
|
40
|
1 |
|
return false; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
3 |
|
public function verify($password, $hash) { |
|
47
|
3 |
|
list($stored_hash, $salt) = $this->splitHash($hash); |
|
48
|
3 |
|
$calc_hash = $this->hashRaw($password, $salt); |
|
49
|
3 |
|
return $calc_hash === $stored_hash; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Split the hash into its calculated hash and salt. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $hash The hash to split. |
|
56
|
|
|
* @return array An array in the form [$hash, $salt]. |
|
57
|
|
|
*/ |
|
58
|
3 |
|
protected function splitHash($hash) { |
|
59
|
3 |
|
if (strpos($hash, ':') === false) { |
|
60
|
1 |
|
return [false, false]; |
|
61
|
|
|
} else { |
|
62
|
2 |
|
$parts = explode(':', $hash, 2); |
|
63
|
2 |
|
return $parts; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|