Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 126 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| PasswordHash | |
0.00% |
0 / 126 |
|
0.00% |
0 / 9 |
1640 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| get_random_bytes | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
42 | |||
| encode64 | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
30 | |||
| gensalt_private | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| crypt_private | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
72 | |||
| gensalt_extended | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| gensalt_blowfish | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
6 | |||
| HashPassword | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
110 | |||
| CheckPassword | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\Core; |
| 4 | |
| 5 | /** |
| 6 | * |
| 7 | * Portable PHP password hashing framework. |
| 8 | * |
| 9 | * Version 1.0.0 - modified by Nordstromrack.com | HauteLook |
| 10 | * |
| 11 | * Change Log: |
| 12 | * |
| 13 | * - the hash_equals function is now used instead of == or === to prevent |
| 14 | * timing attacks |
| 15 | * |
| 16 | * Written by Solar Designer <solar at openwall.com> in 2004-2006 and placed in |
| 17 | * |
| 18 | * There's absolutely no warranty. |
| 19 | * |
| 20 | * The homepage URL for this framework is: |
| 21 | * |
| 22 | * http://www.openwall.com/phpass/ |
| 23 | * |
| 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. |
| 27 | * |
| 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. |
| 31 | * |
| 32 | * Obviously, since this code is in the public domain, the above are not |
| 33 | * requirements (there can be none), but merely suggestions. |
| 34 | * |
| 35 | * @author Solar Designer <solar@openwall.com> |
| 36 | * @package BuyerKiosk\Core |
| 37 | */ |
| 38 | class PasswordHash |
| 39 | { |
| 40 | private $itoa64; |
| 41 | private $iteration_count_log2; |
| 42 | private $portable_hashes; |
| 43 | private $random_state; |
| 44 | |
| 45 | /** |
| 46 | * Constructor |
| 47 | * |
| 48 | * @param int $iteration_count_log2 |
| 49 | * @param boolean $portable_hashes |
| 50 | */ |
| 51 | public function __construct($iteration_count_log2, $portable_hashes) |
| 52 | { |
| 53 | $this->itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; |
| 54 | |
| 55 | if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31) { |
| 56 | $iteration_count_log2 = 8; |
| 57 | } |
| 58 | $this->iteration_count_log2 = $iteration_count_log2; |
| 59 | |
| 60 | $this->portable_hashes = $portable_hashes; |
| 61 | |
| 62 | $this->random_state = microtime(); |
| 63 | if (function_exists('getmypid')) { |
| 64 | $this->random_state .= getmypid(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @param int $count |
| 70 | * @return String |
| 71 | */ |
| 72 | public function get_random_bytes($count) |
| 73 | { |
| 74 | $output = ''; |
| 75 | |
| 76 | if (is_callable('random_bytes')) { |
| 77 | return random_bytes($count); |
| 78 | } |
| 79 | |
| 80 | if (@is_readable('/dev/urandom') && |
| 81 | ($fh = @fopen('/dev/urandom', 'rb'))) { |
| 82 | $output = fread($fh, $count); |
| 83 | fclose($fh); |
| 84 | } |
| 85 | |
| 86 | if (strlen($output) < $count) { |
| 87 | $output = ''; |
| 88 | for ($i = 0; $i < $count; $i += 16) { |
| 89 | $this->random_state = |
| 90 | md5(microtime() . $this->random_state); |
| 91 | $output .= |
| 92 | pack('H*', md5($this->random_state)); |
| 93 | } |
| 94 | $output = substr($output, 0, $count); |
| 95 | } |
| 96 | |
| 97 | return $output; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @param String $input |
| 102 | * @param int $count |
| 103 | * @return String |
| 104 | */ |
| 105 | public function encode64($input, $count) |
| 106 | { |
| 107 | $output = ''; |
| 108 | $i = 0; |
| 109 | do { |
| 110 | $value = ord($input[$i++]); |
| 111 | $output .= $this->itoa64[$value & 0x3f]; |
| 112 | if ($i < $count) { |
| 113 | $value |= ord($input[$i]) << 8; |
| 114 | } |
| 115 | $output .= $this->itoa64[($value >> 6) & 0x3f]; |
| 116 | if ($i++ >= $count) { |
| 117 | break; |
| 118 | } |
| 119 | if ($i < $count) { |
| 120 | $value |= ord($input[$i]) << 16; |
| 121 | } |
| 122 | $output .= $this->itoa64[($value >> 12) & 0x3f]; |
| 123 | if ($i++ >= $count) { |
| 124 | break; |
| 125 | } |
| 126 | $output .= $this->itoa64[($value >> 18) & 0x3f]; |
| 127 | } while ($i < $count); |
| 128 | |
| 129 | return $output; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @param String $input |
| 134 | * @return String |
| 135 | */ |
| 136 | public function gensalt_private($input) |
| 137 | { |
| 138 | $output = '$P$'; |
| 139 | $output .= $this->itoa64[min($this->iteration_count_log2 + |
| 140 | ((PHP_VERSION >= '5') ? 5 : 3), 30)]; |
| 141 | $output .= $this->encode64($input, 6); |
| 142 | |
| 143 | return $output; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @param String $password |
| 148 | * @param String $setting |
| 149 | * @return String |
| 150 | */ |
| 151 | public function crypt_private($password, $setting) |
| 152 | { |
| 153 | $output = '*0'; |
| 154 | if (substr($setting, 0, 2) == $output) { |
| 155 | $output = '*1'; |
| 156 | } |
| 157 | |
| 158 | $id = substr($setting, 0, 3); |
| 159 | # We use "$P$", phpBB3 uses "$H$" for the same thing |
| 160 | if ($id != '$P$' && $id != '$H$') { |
| 161 | return $output; |
| 162 | } |
| 163 | |
| 164 | $count_log2 = strpos($this->itoa64, $setting[3]); |
| 165 | if ($count_log2 < 7 || $count_log2 > 30) { |
| 166 | return $output; |
| 167 | } |
| 168 | |
| 169 | $count = 1 << $count_log2; |
| 170 | |
| 171 | $salt = substr($setting, 4, 8); |
| 172 | if (strlen($salt) != 8) { |
| 173 | return $output; |
| 174 | } |
| 175 | |
| 176 | // We're kind of forced to use MD5 here since it's the only |
| 177 | // cryptographic primitive available in all versions of PHP |
| 178 | // currently in use. To implement our own low-level crypto |
| 179 | // in PHP would result in much worse performance and |
| 180 | // consequently in lower iteration counts and hashes that are |
| 181 | // quicker to crack (by non-PHP code). |
| 182 | if (PHP_VERSION >= '5') { |
| 183 | $hash = md5($salt . $password, TRUE); |
| 184 | do { |
| 185 | $hash = md5($hash . $password, TRUE); |
| 186 | } while (--$count); |
| 187 | } else { |
| 188 | $hash = pack('H*', md5($salt . $password)); |
| 189 | do { |
| 190 | $hash = pack('H*', md5($hash . $password)); |
| 191 | } while (--$count); |
| 192 | } |
| 193 | |
| 194 | $output = substr($setting, 0, 12); |
| 195 | $output .= $this->encode64($hash, 16); |
| 196 | |
| 197 | return $output; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * @param String $input |
| 202 | * @return String |
| 203 | */ |
| 204 | public function gensalt_extended($input) |
| 205 | { |
| 206 | $count_log2 = min($this->iteration_count_log2 + 8, 24); |
| 207 | // This should be odd to not reveal weak DES keys, and the |
| 208 | // maximum valid value is (2**24 - 1) which is odd anyway. |
| 209 | $count = (1 << $count_log2) - 1; |
| 210 | |
| 211 | $output = '_'; |
| 212 | $output .= $this->itoa64[$count & 0x3f]; |
| 213 | $output .= $this->itoa64[($count >> 6) & 0x3f]; |
| 214 | $output .= $this->itoa64[($count >> 12) & 0x3f]; |
| 215 | $output .= $this->itoa64[($count >> 18) & 0x3f]; |
| 216 | |
| 217 | $output .= $this->encode64($input, 3); |
| 218 | |
| 219 | return $output; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * @param String $input |
| 224 | * @return String |
| 225 | */ |
| 226 | public function gensalt_blowfish($input) |
| 227 | { |
| 228 | // This one needs to use a different order of characters and a |
| 229 | // different encoding scheme from the one in encode64() above. |
| 230 | // We care because the last character in our encoded string will |
| 231 | // only represent 2 bits. While two known implementations of |
| 232 | // bcrypt will happily accept and correct a salt string which |
| 233 | // has the 4 unused bits set to non-zero, we do not want to take |
| 234 | // chances and we also do not want to waste an additional byte |
| 235 | // of entropy. |
| 236 | $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
| 237 | |
| 238 | $output = '$2a$'; |
| 239 | $output .= chr(ord('0') + $this->iteration_count_log2 / 10); |
| 240 | $output .= chr(ord('0') + $this->iteration_count_log2 % 10); |
| 241 | $output .= '$'; |
| 242 | |
| 243 | $i = 0; |
| 244 | do { |
| 245 | $c1 = ord($input[$i++]); |
| 246 | $output .= $itoa64[$c1 >> 2]; |
| 247 | $c1 = ($c1 & 0x03) << 4; |
| 248 | if ($i >= 16) { |
| 249 | $output .= $itoa64[$c1]; |
| 250 | break; |
| 251 | } |
| 252 | |
| 253 | $c2 = ord($input[$i++]); |
| 254 | $c1 |= $c2 >> 4; |
| 255 | $output .= $itoa64[$c1]; |
| 256 | $c1 = ($c2 & 0x0f) << 2; |
| 257 | |
| 258 | $c2 = ord($input[$i++]); |
| 259 | $c1 |= $c2 >> 6; |
| 260 | $output .= $itoa64[$c1]; |
| 261 | $output .= $itoa64[$c2 & 0x3f]; |
| 262 | } while (1); |
| 263 | |
| 264 | return $output; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * @param String $password |
| 269 | */ |
| 270 | public function HashPassword($password) |
| 271 | { |
| 272 | $random = ''; |
| 273 | |
| 274 | if (CRYPT_BLOWFISH == 1 && !$this->portable_hashes) { |
| 275 | $random = $this->get_random_bytes(16); |
| 276 | $hash = |
| 277 | crypt($password, $this->gensalt_blowfish($random)); |
| 278 | if (strlen($hash) == 60) { |
| 279 | return $hash; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | if (CRYPT_EXT_DES == 1 && !$this->portable_hashes) { |
| 284 | if (strlen($random) < 3) { |
| 285 | $random = $this->get_random_bytes(3); |
| 286 | } |
| 287 | $hash = |
| 288 | crypt($password, $this->gensalt_extended($random)); |
| 289 | if (strlen($hash) == 20) { |
| 290 | return $hash; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | if (strlen($random) < 6) { |
| 295 | $random = $this->get_random_bytes(6); |
| 296 | } |
| 297 | |
| 298 | $hash = |
| 299 | $this->crypt_private($password, |
| 300 | $this->gensalt_private($random)); |
| 301 | if (strlen($hash) == 34) { |
| 302 | return $hash; |
| 303 | } |
| 304 | |
| 305 | // Returning '*' on error is safe here, but would _not_ be safe |
| 306 | // in a crypt(3)-like function used _both_ for generating new |
| 307 | // hashes and for validating passwords against existing hashes. |
| 308 | return '*'; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * @param String $password |
| 313 | * @param String $stored_hash |
| 314 | * @return boolean |
| 315 | */ |
| 316 | public function CheckPassword($password, $stored_hash) |
| 317 | { |
| 318 | $hash = $this->crypt_private($password, $stored_hash); |
| 319 | if ($hash[0] == '*') { |
| 320 | $hash = crypt($password, $stored_hash); |
| 321 | } |
| 322 | |
| 323 | return hash_equals($stored_hash, $hash); |
| 324 | } |
| 325 | } |