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