wp/wp-includes/class-phpass.php
author ymh <ymh.work@gmail.com>
Wed, 06 Nov 2013 03:21:17 +0000
changeset 0 d970ebf37754
child 5 5e2f62d02dcd
permissions -rw-r--r--
first import
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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @since 2.5
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * @version 0.3 / WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * @link http://www.openwall.com/phpass/
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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
 * @link http://www.openwall.com/phpass/
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
 * @since 2.5
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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
	function PasswordHash($iteration_count_log2, $portable_hashes)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
	{
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
		$this->itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
		if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
			$iteration_count_log2 = 8;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
		$this->iteration_count_log2 = $iteration_count_log2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
		$this->portable_hashes = $portable_hashes;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
		$this->random_state = microtime() . uniqid(rand(), TRUE); // removed getmypid() for compatibility reasons
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
	function get_random_bytes($count)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
	{
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
		$output = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
		if ( @is_readable('/dev/urandom') &&
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
		    ($fh = @fopen('/dev/urandom', 'rb'))) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
			$output = fread($fh, $count);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
			fclose($fh);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
		if (strlen($output) < $count) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
			$output = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
			for ($i = 0; $i < $count; $i += 16) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
				$this->random_state =
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
				    md5(microtime() . $this->random_state);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
				$output .=
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
				    pack('H*', md5($this->random_state));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
			$output = substr($output, 0, $count);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
	function encode64($input, $count)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
	{
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
		$output = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
		$i = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
		do {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
			$value = ord($input[$i++]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
			$output .= $this->itoa64[$value & 0x3f];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
			if ($i < $count)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
				$value |= ord($input[$i]) << 8;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
			$output .= $this->itoa64[($value >> 6) & 0x3f];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
			if ($i++ >= $count)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
			if ($i < $count)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
				$value |= ord($input[$i]) << 16;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
			$output .= $this->itoa64[($value >> 12) & 0x3f];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
			if ($i++ >= $count)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
			$output .= $this->itoa64[($value >> 18) & 0x3f];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
		} while ($i < $count);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
		return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
	function gensalt_private($input)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
	{
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
		$output = '$P$';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
		$output .= $this->itoa64[min($this->iteration_count_log2 +
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
			((PHP_VERSION >= '5') ? 5 : 3), 30)];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
		$output .= $this->encode64($input, 6);
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 crypt_private($password, $setting)
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 = '*0';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
		if (substr($setting, 0, 2) == $output)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
			$output = '*1';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
		$id = substr($setting, 0, 3);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
		# We use "$P$", phpBB3 uses "$H$" for the same thing
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
		if ($id != '$P$' && $id != '$H$')
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
			return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
		$count_log2 = strpos($this->itoa64, $setting[3]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
		if ($count_log2 < 7 || $count_log2 > 30)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
			return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
		$count = 1 << $count_log2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
		$salt = substr($setting, 4, 8);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
		if (strlen($salt) != 8)
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
		# 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
   133
		# cryptographic primitive available in all versions of PHP
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
		# currently in use.  To implement our own low-level crypto
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
		# in PHP would result in much worse performance and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
		# consequently in lower iteration counts and hashes that are
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
		# quicker to crack (by non-PHP code).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
		if (PHP_VERSION >= '5') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
			$hash = md5($salt . $password, TRUE);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
			do {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
				$hash = md5($hash . $password, TRUE);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
			} while (--$count);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
			$hash = pack('H*', md5($salt . $password));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
			do {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
				$hash = pack('H*', md5($hash . $password));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
			} while (--$count);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
		$output = substr($setting, 0, 12);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
		$output .= $this->encode64($hash, 16);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
		return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
	function gensalt_extended($input)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
	{
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
		$count_log2 = min($this->iteration_count_log2 + 8, 24);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
		# This should be odd to not reveal weak DES keys, and the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
		# maximum valid value is (2**24 - 1) which is odd anyway.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
		$count = (1 << $count_log2) - 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
		$output = '_';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
		$output .= $this->itoa64[$count & 0x3f];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
		$output .= $this->itoa64[($count >> 6) & 0x3f];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
		$output .= $this->itoa64[($count >> 12) & 0x3f];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
		$output .= $this->itoa64[($count >> 18) & 0x3f];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
		$output .= $this->encode64($input, 3);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
		return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
	function gensalt_blowfish($input)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
	{
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
		# This one needs to use a different order of characters and a
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
		# different encoding scheme from the one in encode64() above.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
		# We care because the last character in our encoded string will
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
		# only represent 2 bits.  While two known implementations of
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
		# bcrypt will happily accept and correct a salt string which
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
		# 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
   182
		# chances and we also do not want to waste an additional byte
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
		# of entropy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
		$itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
		$output = '$2a$';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
		$output .= chr(ord('0') + $this->iteration_count_log2 / 10);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
		$output .= chr(ord('0') + $this->iteration_count_log2 % 10);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
		$output .= '$';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
		$i = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
		do {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
			$c1 = ord($input[$i++]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
			$output .= $itoa64[$c1 >> 2];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
			$c1 = ($c1 & 0x03) << 4;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
			if ($i >= 16) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
				$output .= $itoa64[$c1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
			$c2 = ord($input[$i++]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
			$c1 |= $c2 >> 4;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
			$output .= $itoa64[$c1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
			$c1 = ($c2 & 0x0f) << 2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
			$c2 = ord($input[$i++]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
			$c1 |= $c2 >> 6;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
			$output .= $itoa64[$c1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
			$output .= $itoa64[$c2 & 0x3f];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
		} while (1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
		return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
	function HashPassword($password)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
	{
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
		$random = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
		if (CRYPT_BLOWFISH == 1 && !$this->portable_hashes) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
			$random = $this->get_random_bytes(16);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
			$hash =
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
			    crypt($password, $this->gensalt_blowfish($random));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
			if (strlen($hash) == 60)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
				return $hash;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
		if (CRYPT_EXT_DES == 1 && !$this->portable_hashes) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
			if (strlen($random) < 3)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
				$random = $this->get_random_bytes(3);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
			$hash =
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
			    crypt($password, $this->gensalt_extended($random));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
			if (strlen($hash) == 20)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
				return $hash;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
		if (strlen($random) < 6)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
			$random = $this->get_random_bytes(6);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
		$hash =
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
		    $this->crypt_private($password,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
		    $this->gensalt_private($random));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
		if (strlen($hash) == 34)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
			return $hash;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
		# Returning '*' on error is safe here, but would _not_ be safe
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
		# in a crypt(3)-like function used _both_ for generating new
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
		# hashes and for validating passwords against existing hashes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
		return '*';
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
	function CheckPassword($password, $stored_hash)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
	{
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
		$hash = $this->crypt_private($password, $stored_hash);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
		if ($hash[0] == '*')
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
			$hash = crypt($password, $stored_hash);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
		return $hash === $stored_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
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
?>