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