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 | 6 | public function verify($plaintext, $hashvalue, array $extra = array()) |
|
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) |
|
69 | { |
||
70 | 1 | $hex = sha1($plaintext, true); |
|
71 | 1 | $computed_hash = '{SHA}' . base64_encode($hex); |
|
72 | 1 | return $computed_hash === $hashvalue; |
|
73 | } |
||
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) |
|
87 | { |
||
88 | 1 | $salt = preg_replace('/^\$apr1\$([^$]+)\$.*/', '\\1', $hashvalue); |
|
89 | 1 | $context = $this->computeContext($plaintext, $salt); |
|
90 | 1 | $binary = $this->computeBinary($plaintext, $salt, $context); |
|
91 | 1 | $p = $this->computeP($binary); |
|
92 | 1 | $computed_hash = '$apr1$' . $salt . '$' . $p |
|
93 | 1 | . $this->convert64(ord($binary[11]), 3); |
|
94 | 1 | return $computed_hash === $hashvalue; |
|
95 | } |
||
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 |