1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Copyright (c) Phauthentic (https://github.com/Phauthentic) |
5
|
|
|
* |
6
|
|
|
* Licensed under The MIT License |
7
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
8
|
|
|
* Redistributions of files must retain the above copyright notice. |
9
|
|
|
* |
10
|
|
|
* @copyright Copyright (c) Phauthentic (https://github.com/Phauthentic) |
11
|
|
|
* @link https://github.com/Phauthentic |
12
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
13
|
|
|
*/ |
14
|
|
|
namespace Phauthentic\PasswordHasher; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Md5 Password Hasher |
18
|
|
|
* |
19
|
|
|
* WARNING: This is UNSAFE and should NOT be used anymore!!! md5 has been |
20
|
|
|
* compromised! It is no longer a secure hash algorithm! |
21
|
|
|
* |
22
|
|
|
* This hasher just exists for legacy reasons and is thought to be used with |
23
|
|
|
* the Fallback hasher, to update the passwords transparently to a secure hash. |
24
|
|
|
*/ |
25
|
|
|
class Md5PasswordHasher extends AbstractPasswordHasher |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Raw sha1 output |
29
|
|
|
* |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
protected $rawOutput = false; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Sets the raw output mode of sha1 |
36
|
|
|
* |
37
|
|
|
* @link http://php.net/manual/en/function.sha1.php |
38
|
|
|
* @param bool $rawOutput Raw output |
39
|
|
|
* @return $this |
40
|
|
|
*/ |
41
|
|
|
public function setRawOutput(bool $rawOutput): self |
42
|
|
|
{ |
43
|
|
|
$this->rawOutput = $rawOutput; |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Generates password hash. |
50
|
|
|
* |
51
|
|
|
* @param string $password Plain text password to hash required to generate |
52
|
|
|
* password hash. |
53
|
|
|
* @return string Password hash |
54
|
|
|
*/ |
55
|
4 |
|
public function hash(string $password): string |
56
|
|
|
{ |
57
|
4 |
|
if (!empty($this->salt)) { |
58
|
1 |
|
if ($this->saltPosition === self::SALT_BEFORE) { |
59
|
1 |
|
$password = $this->salt . $password; |
60
|
|
|
} else { |
61
|
1 |
|
$password = $password . $this->salt; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
4 |
|
return $this->callHashFunction($password); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Calls the actual php method that will do the hashing |
70
|
|
|
* |
71
|
|
|
* @param string $password Password string |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
2 |
|
protected function callHashFunction(string $password): string |
75
|
|
|
{ |
76
|
2 |
|
return md5($password, $this->rawOutput); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Check hash. Generate hash from user provided password string |
81
|
|
|
* and check against existing hash. |
82
|
|
|
* |
83
|
|
|
* @param string $password Plain text password to hash. |
84
|
|
|
* @param string $hashedPassword Existing hashed password. |
85
|
|
|
* @return bool True if hashes match else false. |
86
|
|
|
*/ |
87
|
2 |
|
public function check(string $password, string $hashedPassword): bool |
88
|
|
|
{ |
89
|
2 |
|
return $this->hash($password) === $hashedPassword; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|