1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
5
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
6
|
|
|
* |
7
|
|
|
* Licensed under The MIT License |
8
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
10
|
|
|
* |
11
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
12
|
|
|
* @link http://cakephp.org CakePHP(tm) Project |
13
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
14
|
|
|
*/ |
15
|
|
|
namespace Phauthentic\PasswordHasher; |
16
|
|
|
|
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Abstract password hashing class |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractPasswordHasher implements PasswordHasherInterface |
23
|
|
|
{ |
24
|
|
|
const SALT_BEFORE = 'before'; |
25
|
|
|
const SALT_AFTER = 'after'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Salt |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
protected $salt = ''; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Position of the salt |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $saltPosition; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Sets the salt if any was used |
43
|
|
|
* |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
3 |
|
public function setSalt(string $salt, string $position = self::SALT_AFTER): self |
47
|
|
|
{ |
48
|
3 |
|
$this->checkSaltPositionArgument($position); |
49
|
2 |
|
$this->salt = $salt; |
50
|
2 |
|
$this->saltPosition = $position; |
51
|
|
|
|
52
|
2 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Checks the salt position argument |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
3 |
|
protected function checkSaltPositionArgument($position): void |
61
|
|
|
{ |
62
|
3 |
|
if (!in_array($position, [self::SALT_BEFORE, self::SALT_AFTER])) { |
63
|
1 |
|
throw new InvalidArgumentException(sprintf( |
64
|
1 |
|
'`%s` is an invalud argument, it has to be `` or ``', |
65
|
1 |
|
$position, |
66
|
1 |
|
self::SALT_BEFORE, |
67
|
1 |
|
self::SALT_AFTER |
68
|
|
|
)); |
69
|
|
|
} |
70
|
2 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns true if the password need to be rehashed, due to the password being |
74
|
|
|
* created with anything else than the passwords generated by this class. |
75
|
|
|
* |
76
|
|
|
* Returns true by default since the only implementation users should rely |
77
|
|
|
* on is the one provided by default in php 5.5+ or any compatible library |
78
|
|
|
* |
79
|
|
|
* @param string $password The password to verify |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
1 |
|
public function needsRehash(string $password): bool |
83
|
|
|
{ |
84
|
1 |
|
return password_needs_rehash($password, PASSWORD_DEFAULT); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|