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