1 | <?php |
||
21 | class Password |
||
22 | { |
||
23 | use ClassOptionsTrait; |
||
24 | |||
25 | /** |
||
26 | * @var array An associative array containing options |
||
27 | * |
||
28 | * http://php.net/manual/en/password.constants.php |
||
29 | */ |
||
30 | protected $options = [ |
||
31 | 'cost' => 11, |
||
32 | 'algo' => PASSWORD_DEFAULT, |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * Class constructor. |
||
37 | * <p><b>$options valid keys:</b></p> |
||
38 | * <table class="parameter"> |
||
39 | * <thead> |
||
40 | * <tr> |
||
41 | * <th>Name</th> |
||
42 | * <th>Default</th> |
||
43 | * <th>Description</th> |
||
44 | * </tr> |
||
45 | * </thead> |
||
46 | * <tbody> |
||
47 | * <tr> |
||
48 | * <td>cost</td> |
||
49 | * <td>11</td> |
||
50 | * <td>indicating key expansion rounds</td> |
||
51 | * </tr> |
||
52 | * <tr> |
||
53 | * <td>algo</td> |
||
54 | * <td>PASSWORD_DEFAULT</td> |
||
55 | * <td>password algorithm denoting the algorithm to use when hashing the password</td> |
||
56 | * </tr> |
||
57 | * </tbody> |
||
58 | * </table> |
||
59 | * <p>For password algorithm constants see <a href="http://php.net/manual/en/password.constants.php">Password Constants</a>.</p> |
||
60 | * <pre><code class="php">//Options passed to class constructor as ['key' => 'value'] array. |
||
61 | * $password = new Password([ |
||
62 | * 'cost' => 11, |
||
63 | * 'algo' => PASSWORD_DEFAULT, |
||
64 | * ]); |
||
65 | * </code></pre> |
||
66 | * |
||
67 | * @param array $options |
||
68 | */ |
||
69 | 70 | public function __construct(array $options = []) |
|
74 | |||
75 | /** |
||
76 | * Verifies if a password matches an hash and return the result as boolean. |
||
77 | * <pre><code class="php">$password = new Password(); |
||
78 | * |
||
79 | * $storedHash = '$2y$11$cq3ZWO18l68X7pGs9Y1fveTGcNJ/iyehrDZ10BAvbY8LaBXNvnyk6'; |
||
80 | * $password = 'FooPassword'; |
||
81 | * |
||
82 | * $verified = $password->verify($password, $storedHash); |
||
83 | * </code></pre> |
||
84 | * |
||
85 | * @param string $password |
||
86 | * @param string $hash |
||
87 | * |
||
88 | * @return bool True if password match, false if not. |
||
89 | */ |
||
90 | 44 | public function verify(string $password, string $hash): bool |
|
94 | |||
95 | /** |
||
96 | * Create password hash from the given string and return it. |
||
97 | * <pre><code class="php">$password = new Password(); |
||
98 | * |
||
99 | * $hash = $password->hash('FooPassword'); |
||
100 | * |
||
101 | * //var_dump result |
||
102 | * //$2y$11$cq3ZWO18l68X7pGs9Y1fveTGcNJ/iyehrDZ10BAvbY8LaBXNvnyk6 |
||
103 | * var_dump($hash) |
||
104 | * </code></pre> |
||
105 | * |
||
106 | * @param string $password |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | 21 | public function hash(string $password): string |
|
115 | |||
116 | /** |
||
117 | * Checks if the given hash matches the algorithm and the options provided. |
||
118 | * <pre><code class="php">$password = new Password(); |
||
119 | * |
||
120 | * $hash = '$2y$11$cq3ZWO18l68X7pGs9Y1fveTGcNJ/iyehrDZ10BAvbY8LaBXNvnyk6'; |
||
121 | * |
||
122 | * //true if rehash is needed, false if no |
||
123 | * $rehashCheck = $password->needsRehash($hash); |
||
124 | * </code></pre> |
||
125 | * |
||
126 | * @param string $hash |
||
127 | * |
||
128 | * @return bool |
||
129 | */ |
||
130 | 2 | public function needsRehash(string $hash): bool |
|
134 | |||
135 | /** |
||
136 | * Returns information about the given hash. |
||
137 | * <pre><code class="php">$password = new Password(); |
||
138 | * |
||
139 | * $hash = '$2y$11$cq3ZWO18l68X7pGs9Y1fveTGcNJ/iyehrDZ10BAvbY8LaBXNvnyk6'; |
||
140 | * |
||
141 | * $info = $password->getInfo($hash); |
||
142 | * |
||
143 | * //var_dump result |
||
144 | * //[ |
||
145 | * // 'algo' => 1, |
||
146 | * // 'algoName' => 'bcrypt', |
||
147 | * // 'options' => [ |
||
148 | * // 'cost' => int 11 |
||
149 | * // ] |
||
150 | * //] |
||
151 | * var_dump($info); |
||
152 | * </code></pre> |
||
153 | * |
||
154 | * @param string $hash |
||
155 | * |
||
156 | * @return array |
||
157 | */ |
||
158 | 2 | public function getInfo(string $hash) : array |
|
162 | } |
||
163 |