1 | <?php |
||
23 | class HtpasswdVerifier implements VerifierInterface |
||
24 | { |
||
25 | /** |
||
26 | * |
||
27 | * Verifies a plaintext password against a hash. |
||
28 | * |
||
29 | * @param string $plaintext Plaintext password. |
||
30 | * |
||
31 | * @param string $hashvalue Comparison hash. |
||
32 | * |
||
33 | * @param array $extra Optional array if used by verify. |
||
34 | * |
||
35 | * @return bool |
||
36 | * |
||
37 | */ |
||
38 | 5 | public function verify($plaintext, $hashvalue, array $extra = array()) |
|
39 | { |
||
40 | 5 | $hashvalue = trim($hashvalue); |
|
41 | |||
42 | 5 | if (substr($hashvalue, 0, 4) == '$2y$') { |
|
43 | return password_verify($plaintext, $hashvalue); |
||
44 | } |
||
45 | |||
46 | 5 | if (substr($hashvalue, 0, 5) == '{SHA}') { |
|
47 | 1 | return $this->sha($plaintext, $hashvalue); |
|
48 | } |
||
49 | |||
50 | 4 | if (substr($hashvalue, 0, 6) == '$apr1$') { |
|
51 | 1 | return $this->apr1($plaintext, $hashvalue); |
|
52 | } |
||
53 | |||
54 | 3 | return $this->des($plaintext, $hashvalue); |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * |
||
59 | * Verify using SHA1 hashing. |
||
60 | * |
||
61 | * @param string $plaintext Plaintext password. |
||
62 | * |
||
63 | * @param string $hashvalue Comparison hash. |
||
64 | * |
||
65 | * @return bool |
||
66 | * |
||
67 | */ |
||
68 | 1 | protected function sha($plaintext, $hashvalue) |
|
74 | |||
75 | /** |
||
76 | * |
||
77 | * Verify using APR compatible MD5 hashing. |
||
78 | * |
||
79 | * @param string $plaintext Plaintext password. |
||
80 | * |
||
81 | * @param string $hashvalue Comparison hash. |
||
82 | * |
||
83 | * @return bool |
||
84 | * |
||
85 | */ |
||
86 | 1 | protected function apr1($plaintext, $hashvalue) |
|
96 | |||
97 | /** |
||
98 | * |
||
99 | * Compute the context. |
||
100 | * |
||
101 | * @param string $plaintext Plaintext password. |
||
102 | * |
||
103 | * @param string $salt The salt. |
||
104 | * |
||
105 | * @return string |
||
106 | * |
||
107 | */ |
||
108 | 1 | protected function computeContext($plaintext, $salt) |
|
124 | |||
125 | /** |
||
126 | * |
||
127 | * Compute the binary. |
||
128 | * |
||
129 | * @param string $plaintext Plaintext password. |
||
130 | * |
||
131 | * @param string $salt The salt. |
||
132 | * |
||
133 | * @param string $context The context. |
||
134 | * |
||
135 | * @return string |
||
136 | * |
||
137 | */ |
||
138 | 1 | protected function computeBinary($plaintext, $salt, $context) |
|
154 | |||
155 | /** |
||
156 | * |
||
157 | * Compute the P value for a binary. |
||
158 | * |
||
159 | * @param string $binary The binary. |
||
160 | * |
||
161 | * @return string |
||
162 | * |
||
163 | */ |
||
164 | 1 | protected function computeP($binary) |
|
182 | |||
183 | /** |
||
184 | * |
||
185 | * Convert to allowed 64 characters for encryption. |
||
186 | * |
||
187 | * @param string $value The value to convert. |
||
188 | * |
||
189 | * @param int $count The number of characters. |
||
190 | * |
||
191 | * @return string The converted value. |
||
192 | * |
||
193 | */ |
||
194 | 1 | protected function convert64($value, $count) |
|
204 | |||
205 | /** |
||
206 | * |
||
207 | * Verify using DES hashing. |
||
208 | * |
||
209 | * Note that crypt() will only check up to the first 8 |
||
210 | * characters of a password; chars after 8 are ignored. This |
||
211 | * means that if the real password is "atecharsnine", the |
||
212 | * word "atechars" would be valid. This is bad. As a |
||
213 | * workaround, if the password provided by the user is |
||
214 | * longer than 8 characters, this method will *not* verify |
||
215 | * it. |
||
216 | * |
||
217 | * @param string $plaintext Plaintext password. |
||
218 | * |
||
219 | * @param string $hashvalue Comparison hash. |
||
220 | * |
||
221 | * @return bool |
||
222 | * |
||
223 | */ |
||
224 | 3 | protected function des($plaintext, $hashvalue) |
|
233 | } |
||
234 |