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