3 * Portable PHP password hashing framework.
6 * @version 0.3 / WordPress
7 * @link http://www.openwall.com/phpass/
11 # Written by Solar Designer <solar at openwall.com> in 2004-2006 and placed in
12 # the public domain. Revised in subsequent years, still public domain.
14 # There's absolutely no warranty.
16 # Please be sure to update the Version line if you edit this file in any way.
17 # It is suggested that you leave the main version number intact, but indicate
18 # your project name (after the slash) and add your own revision information.
20 # Please do not change the "private" password hashing method implemented in
21 # here, thereby making your hashes incompatible. However, if you must, please
22 # change the hash type identifier (the "$P$") to something different.
24 # Obviously, since this code is in the public domain, the above are not
25 # requirements (there can be none), but merely suggestions.
29 * Portable PHP password hashing framework.
32 * @version 0.3 / WordPress
33 * @link http://www.openwall.com/phpass/
38 var $iteration_count_log2;
42 function PasswordHash($iteration_count_log2, $portable_hashes)
44 $this->itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
46 if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31)
47 $iteration_count_log2 = 8;
48 $this->iteration_count_log2 = $iteration_count_log2;
50 $this->portable_hashes = $portable_hashes;
52 $this->random_state = microtime() . uniqid(rand(), TRUE); // removed getmypid() for compatibility reasons
55 function get_random_bytes($count)
58 if ( @is_readable('/dev/urandom') &&
59 ($fh = @fopen('/dev/urandom', 'rb'))) {
60 $output = fread($fh, $count);
64 if (strlen($output) < $count) {
66 for ($i = 0; $i < $count; $i += 16) {
68 md5(microtime() . $this->random_state);
70 pack('H*', md5($this->random_state));
72 $output = substr($output, 0, $count);
78 function encode64($input, $count)
83 $value = ord($input[$i++]);
84 $output .= $this->itoa64[$value & 0x3f];
86 $value |= ord($input[$i]) << 8;
87 $output .= $this->itoa64[($value >> 6) & 0x3f];
91 $value |= ord($input[$i]) << 16;
92 $output .= $this->itoa64[($value >> 12) & 0x3f];
95 $output .= $this->itoa64[($value >> 18) & 0x3f];
96 } while ($i < $count);
101 function gensalt_private($input)
104 $output .= $this->itoa64[min($this->iteration_count_log2 +
105 ((PHP_VERSION >= '5') ? 5 : 3), 30)];
106 $output .= $this->encode64($input, 6);
111 function crypt_private($password, $setting)
114 if (substr($setting, 0, 2) == $output)
117 $id = substr($setting, 0, 3);
118 # We use "$P$", phpBB3 uses "$H$" for the same thing
119 if ($id != '$P$' && $id != '$H$')
122 $count_log2 = strpos($this->itoa64, $setting[3]);
123 if ($count_log2 < 7 || $count_log2 > 30)
126 $count = 1 << $count_log2;
128 $salt = substr($setting, 4, 8);
129 if (strlen($salt) != 8)
132 # We're kind of forced to use MD5 here since it's the only
133 # cryptographic primitive available in all versions of PHP
134 # currently in use. To implement our own low-level crypto
135 # in PHP would result in much worse performance and
136 # consequently in lower iteration counts and hashes that are
137 # quicker to crack (by non-PHP code).
138 if (PHP_VERSION >= '5') {
139 $hash = md5($salt . $password, TRUE);
141 $hash = md5($hash . $password, TRUE);
144 $hash = pack('H*', md5($salt . $password));
146 $hash = pack('H*', md5($hash . $password));
150 $output = substr($setting, 0, 12);
151 $output .= $this->encode64($hash, 16);
156 function gensalt_extended($input)
158 $count_log2 = min($this->iteration_count_log2 + 8, 24);
159 # This should be odd to not reveal weak DES keys, and the
160 # maximum valid value is (2**24 - 1) which is odd anyway.
161 $count = (1 << $count_log2) - 1;
164 $output .= $this->itoa64[$count & 0x3f];
165 $output .= $this->itoa64[($count >> 6) & 0x3f];
166 $output .= $this->itoa64[($count >> 12) & 0x3f];
167 $output .= $this->itoa64[($count >> 18) & 0x3f];
169 $output .= $this->encode64($input, 3);
174 function gensalt_blowfish($input)
176 # This one needs to use a different order of characters and a
177 # different encoding scheme from the one in encode64() above.
178 # We care because the last character in our encoded string will
179 # only represent 2 bits. While two known implementations of
180 # bcrypt will happily accept and correct a salt string which
181 # has the 4 unused bits set to non-zero, we do not want to take
182 # chances and we also do not want to waste an additional byte
184 $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
187 $output .= chr(ord('0') + $this->iteration_count_log2 / 10);
188 $output .= chr(ord('0') + $this->iteration_count_log2 % 10);
193 $c1 = ord($input[$i++]);
194 $output .= $itoa64[$c1 >> 2];
195 $c1 = ($c1 & 0x03) << 4;
197 $output .= $itoa64[$c1];
201 $c2 = ord($input[$i++]);
203 $output .= $itoa64[$c1];
204 $c1 = ($c2 & 0x0f) << 2;
206 $c2 = ord($input[$i++]);
208 $output .= $itoa64[$c1];
209 $output .= $itoa64[$c2 & 0x3f];
215 function HashPassword($password)
217 if ( strlen( $password ) > 4096 ) {
223 if (CRYPT_BLOWFISH == 1 && !$this->portable_hashes) {
224 $random = $this->get_random_bytes(16);
226 crypt($password, $this->gensalt_blowfish($random));
227 if (strlen($hash) == 60)
231 if (CRYPT_EXT_DES == 1 && !$this->portable_hashes) {
232 if (strlen($random) < 3)
233 $random = $this->get_random_bytes(3);
235 crypt($password, $this->gensalt_extended($random));
236 if (strlen($hash) == 20)
240 if (strlen($random) < 6)
241 $random = $this->get_random_bytes(6);
243 $this->crypt_private($password,
244 $this->gensalt_private($random));
245 if (strlen($hash) == 34)
248 # Returning '*' on error is safe here, but would _not_ be safe
249 # in a crypt(3)-like function used _both_ for generating new
250 # hashes and for validating passwords against existing hashes.
254 function CheckPassword($password, $stored_hash)
256 if ( strlen( $password ) > 4096 ) {
260 $hash = $this->crypt_private($password, $stored_hash);
262 $hash = crypt($password, $stored_hash);
264 return $hash === $stored_hash;