3 namespace Hautelook\Phpass;
7 * Portable PHP password hashing framework.
9 * Version 1.0.0 - modified by Nordstromrack.com | HauteLook
13 * - the hash_equals function is now used instead of == or === to prevent
16 * Written by Solar Designer <solar at openwall.com> in 2004-2006 and placed in
18 * There's absolutely no warranty.
20 * The homepage URL for this framework is:
22 * http://www.openwall.com/phpass/
24 * Please be sure to update the Version line if you edit this file in any way.
25 * It is suggested that you leave the main version number intact, but indicate
26 * your project name (after the slash) and add your own revision information.
28 * Please do not change the "private" password hashing method implemented in
29 * here, thereby making your hashes incompatible. However, if you must, please
30 * change the hash type identifier (the "$P$") to something different.
32 * Obviously, since this code is in the public domain, the above are not
33 * requirements (there can be none), but merely suggestions.
35 * @author Solar Designer <solar@openwall.com>
40 private $iteration_count_log2;
41 private $portable_hashes;
42 private $random_state;
47 * @param int $iteration_count_log2
48 * @param boolean $portable_hashes
50 public function __construct($iteration_count_log2, $portable_hashes)
52 $this->itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
54 if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31) {
55 $iteration_count_log2 = 8;
57 $this->iteration_count_log2 = $iteration_count_log2;
59 $this->portable_hashes = $portable_hashes;
61 $this->random_state = microtime();
62 if (function_exists('getmypid')) {
63 $this->random_state .= getmypid();
71 public function get_random_bytes($count)
75 if (is_callable('random_bytes')) {
76 return random_bytes($count);
79 if (@is_readable('/dev/urandom') &&
80 ($fh = @fopen('/dev/urandom', 'rb'))) {
81 $output = fread($fh, $count);
85 if (strlen($output) < $count) {
87 for ($i = 0; $i < $count; $i += 16) {
89 md5(microtime() . $this->random_state);
91 pack('H*', md5($this->random_state));
93 $output = substr($output, 0, $count);
100 * @param String $input
104 public function encode64($input, $count)
109 $value = ord($input[$i++]);
110 $output .= $this->itoa64[$value & 0x3f];
112 $value |= ord($input[$i]) << 8;
114 $output .= $this->itoa64[($value >> 6) & 0x3f];
115 if ($i++ >= $count) {
119 $value |= ord($input[$i]) << 16;
121 $output .= $this->itoa64[($value >> 12) & 0x3f];
122 if ($i++ >= $count) {
125 $output .= $this->itoa64[($value >> 18) & 0x3f];
126 } while ($i < $count);
132 * @param String $input
135 public function gensalt_private($input)
138 $output .= $this->itoa64[min($this->iteration_count_log2 +
139 ((PHP_VERSION >= '5') ? 5 : 3), 30)];
140 $output .= $this->encode64($input, 6);
146 * @param String $password
147 * @param String $setting
150 public function crypt_private($password, $setting)
153 if (substr($setting, 0, 2) == $output) {
157 $id = substr($setting, 0, 3);
158 # We use "$P$", phpBB3 uses "$H$" for the same thing
159 if ($id != '$P$' && $id != '$H$') {
163 $count_log2 = strpos($this->itoa64, $setting[3]);
164 if ($count_log2 < 7 || $count_log2 > 30) {
168 $count = 1 << $count_log2;
170 $salt = substr($setting, 4, 8);
171 if (strlen($salt) != 8) {
175 // We're kind of forced to use MD5 here since it's the only
176 // cryptographic primitive available in all versions of PHP
177 // currently in use. To implement our own low-level crypto
178 // in PHP would result in much worse performance and
179 // consequently in lower iteration counts and hashes that are
180 // quicker to crack (by non-PHP code).
181 if (PHP_VERSION >= '5') {
182 $hash = md5($salt . $password, TRUE);
184 $hash = md5($hash . $password, TRUE);
187 $hash = pack('H*', md5($salt . $password));
189 $hash = pack('H*', md5($hash . $password));
193 $output = substr($setting, 0, 12);
194 $output .= $this->encode64($hash, 16);
200 * @param String $input
203 public function gensalt_extended($input)
205 $count_log2 = min($this->iteration_count_log2 + 8, 24);
206 // This should be odd to not reveal weak DES keys, and the
207 // maximum valid value is (2**24 - 1) which is odd anyway.
208 $count = (1 << $count_log2) - 1;
211 $output .= $this->itoa64[$count & 0x3f];
212 $output .= $this->itoa64[($count >> 6) & 0x3f];
213 $output .= $this->itoa64[($count >> 12) & 0x3f];
214 $output .= $this->itoa64[($count >> 18) & 0x3f];
216 $output .= $this->encode64($input, 3);
222 * @param String $input
225 public function gensalt_blowfish($input)
227 // This one needs to use a different order of characters and a
228 // different encoding scheme from the one in encode64() above.
229 // We care because the last character in our encoded string will
230 // only represent 2 bits. While two known implementations of
231 // bcrypt will happily accept and correct a salt string which
232 // has the 4 unused bits set to non-zero, we do not want to take
233 // chances and we also do not want to waste an additional byte
235 $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
238 $output .= chr(ord('0') + $this->iteration_count_log2 / 10);
239 $output .= chr(ord('0') + $this->iteration_count_log2 % 10);
244 $c1 = ord($input[$i++]);
245 $output .= $itoa64[$c1 >> 2];
246 $c1 = ($c1 & 0x03) << 4;
248 $output .= $itoa64[$c1];
252 $c2 = ord($input[$i++]);
254 $output .= $itoa64[$c1];
255 $c1 = ($c2 & 0x0f) << 2;
257 $c2 = ord($input[$i++]);
259 $output .= $itoa64[$c1];
260 $output .= $itoa64[$c2 & 0x3f];
267 * @param String $password
269 public function HashPassword($password)
273 if (CRYPT_BLOWFISH == 1 && !$this->portable_hashes) {
274 $random = $this->get_random_bytes(16);
276 crypt($password, $this->gensalt_blowfish($random));
277 if (strlen($hash) == 60) {
282 if (CRYPT_EXT_DES == 1 && !$this->portable_hashes) {
283 if (strlen($random) < 3) {
284 $random = $this->get_random_bytes(3);
287 crypt($password, $this->gensalt_extended($random));
288 if (strlen($hash) == 20) {
293 if (strlen($random) < 6) {
294 $random = $this->get_random_bytes(6);
298 $this->crypt_private($password,
299 $this->gensalt_private($random));
300 if (strlen($hash) == 34) {
304 // Returning '*' on error is safe here, but would _not_ be safe
305 // in a crypt(3)-like function used _both_ for generating new
306 // hashes and for validating passwords against existing hashes.
311 * @param String $password
312 * @param String $stored_hash
315 public function CheckPassword($password, $stored_hash)
317 $hash = $this->crypt_private($password, $stored_hash);
318 if ($hash[0] == '*') {
319 $hash = crypt($password, $stored_hash);
322 return hash_equals($stored_hash, $hash);