wp/wp-includes/class-phpass.php
author ymh <ymh.work@gmail.com>
Mon, 08 Sep 2025 19:44:41 +0200
changeset 23 417f20492bf7
parent 22 8c2e4d02f4ef
permissions -rw-r--r--
Update Docker configuration and plugin versions - Upgrade MariaDB from 10.6 to 11 with auto-upgrade support - Add WordPress debug environment variable to FPM container - Update PHP-FPM Dockerfile base image - Update Include Mastodon Feed plugin with bug fixes and improvements - Update Portfolio plugin (v2.58) with latest translations and demo data enhancements - Remove old README.md from Mastodon Feed plugin 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
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
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
     6
 * @version 0.5 / 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
#
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    11
# Portable PHP password hashing framework.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    12
#
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
    13
# Version 0.5.4 / WordPress.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    14
#
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
# 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
    16
# the public domain.  Revised in subsequent years, still public domain.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
#
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
# There's absolutely no warranty.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
#
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    20
# The homepage URL for this framework is:
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    21
#
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    22
#	http://www.openwall.com/phpass/
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    23
#
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
# 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
    25
# It is suggested that you leave the main version number intact, but indicate
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
# your project name (after the slash) and add your own revision information.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
#
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
# Please do not change the "private" password hashing method implemented in
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
# here, thereby making your hashes incompatible.  However, if you must, please
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
# change the hash type identifier (the "$P$") to something different.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
#
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
# Obviously, since this code is in the public domain, the above are not
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
# requirements (there can be none), but merely suggestions.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
#
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
 * Portable PHP password hashing framework.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
 * @package phpass
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    40
 * @version 0.5 / WordPress
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    41
 * @link https://www.openwall.com/phpass/
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    42
 * @since 2.5.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
class PasswordHash {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
	var $itoa64;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
	var $iteration_count_log2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
	var $portable_hashes;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
	var $random_state;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    50
	function __construct($iteration_count_log2, $portable_hashes)
0
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->itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
    54
		if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
			$iteration_count_log2 = 8;
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
    56
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
		$this->iteration_count_log2 = $iteration_count_log2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
		$this->portable_hashes = $portable_hashes;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    61
		$this->random_state = microtime();
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
    62
		if (function_exists('getmypid')) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    63
			$this->random_state .= getmypid();
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
    64
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    67
	function PasswordHash($iteration_count_log2, $portable_hashes)
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    68
	{
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    69
		self::__construct($iteration_count_log2, $portable_hashes);
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    70
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    71
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
	function get_random_bytes($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
		$output = '';
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    75
		if (@is_readable('/dev/urandom') &&
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
		    ($fh = @fopen('/dev/urandom', 'rb'))) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
			$output = fread($fh, $count);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
			fclose($fh);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
		if (strlen($output) < $count) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
			$output = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
			for ($i = 0; $i < $count; $i += 16) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
				$this->random_state =
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
				    md5(microtime() . $this->random_state);
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    86
				$output .= md5($this->random_state, TRUE);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
			$output = substr($output, 0, $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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
		return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
	function encode64($input, $count)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
	{
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
		$output = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
		$i = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
		do {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
			$value = ord($input[$i++]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
			$output .= $this->itoa64[$value & 0x3f];
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   101
			if ($i < $count) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
				$value |= ord($input[$i]) << 8;
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   103
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
			$output .= $this->itoa64[($value >> 6) & 0x3f];
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   105
			if ($i++ >= $count) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
				break;
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   107
			}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   108
			if ($i < $count) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
				$value |= ord($input[$i]) << 16;
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   110
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
			$output .= $this->itoa64[($value >> 12) & 0x3f];
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   112
			if ($i++ >= $count) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
				break;
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   114
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
			$output .= $this->itoa64[($value >> 18) & 0x3f];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
		} while ($i < $count);
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 gensalt_private($input)
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 = '$P$';
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   124
		$output .= $this->itoa64[min($this->iteration_count_log2 + 5,
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   125
		    30)];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
		$output .= $this->encode64($input, 6);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
		return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
	function crypt_private($password, $setting)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
	{
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
		$output = '*0';
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   134
		if (substr($setting, 0, 2) === $output) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
			$output = '*1';
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   136
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
		$id = substr($setting, 0, 3);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
		# We use "$P$", phpBB3 uses "$H$" for the same thing
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   140
		if ($id !== '$P$' && $id !== '$H$') {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
			return $output;
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   142
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
		$count_log2 = strpos($this->itoa64, $setting[3]);
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   145
		if ($count_log2 < 7 || $count_log2 > 30) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
			return $output;
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   147
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
		$count = 1 << $count_log2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
		$salt = substr($setting, 4, 8);
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   152
		if (strlen($salt) !== 8) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
			return $output;
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   154
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   156
		# We were kind of forced to use MD5 here since it's the only
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   157
		# cryptographic primitive that was available in all versions
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   158
		# of PHP in use.  To implement our own low-level crypto in PHP
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   159
		# would have resulted in much worse performance and
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
		# consequently in lower iteration counts and hashes that are
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
		# quicker to crack (by non-PHP code).
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   162
		$hash = md5($salt . $password, TRUE);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   163
		do {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   164
			$hash = md5($hash . $password, TRUE);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   165
		} while (--$count);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
		$output = substr($setting, 0, 12);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
		$output .= $this->encode64($hash, 16);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
		return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
	function gensalt_blowfish($input)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
	{
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
		# This one needs to use a different order of characters and a
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
		# different encoding scheme from the one in encode64() above.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
		# We care because the last character in our encoded string will
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
		# only represent 2 bits.  While two known implementations of
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
		# bcrypt will happily accept and correct a salt string which
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
		# 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
   181
		# chances and we also do not want to waste an additional byte
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
		# of entropy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
		$itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
		$output = '$2a$';
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   186
		$output .= chr((int)(ord('0') + $this->iteration_count_log2 / 10));
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   187
		$output .= chr(ord('0') + $this->iteration_count_log2 % 10);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
		$output .= '$';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
		$i = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
		do {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
			$c1 = ord($input[$i++]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
			$output .= $itoa64[$c1 >> 2];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
			$c1 = ($c1 & 0x03) << 4;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
			if ($i >= 16) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
				$output .= $itoa64[$c1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
			$c2 = ord($input[$i++]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
			$c1 |= $c2 >> 4;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
			$output .= $itoa64[$c1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
			$c1 = ($c2 & 0x0f) << 2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
			$c2 = ord($input[$i++]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
			$c1 |= $c2 >> 6;
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
			$output .= $itoa64[$c2 & 0x3f];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
		} while (1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
		return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
	function HashPassword($password)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
	{
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   216
		if ( strlen( $password ) > 4096 ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   217
			return '*';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   218
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   219
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
		$random = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   222
		if (CRYPT_BLOWFISH === 1 && !$this->portable_hashes) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
			$random = $this->get_random_bytes(16);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
			$hash =
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
			    crypt($password, $this->gensalt_blowfish($random));
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   226
			if (strlen($hash) === 60) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
				return $hash;
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   228
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   231
		if (strlen($random) < 6) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
			$random = $this->get_random_bytes(6);
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   233
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
		$hash =
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
		    $this->crypt_private($password,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
		    $this->gensalt_private($random));
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   237
		if (strlen($hash) === 34) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
			return $hash;
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   239
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
		# Returning '*' on error is safe here, but would _not_ be safe
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
		# in a crypt(3)-like function used _both_ for generating new
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
		# hashes and for validating passwords against existing hashes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
		return '*';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
	function CheckPassword($password, $stored_hash)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
	{
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   249
		if ( strlen( $password ) > 4096 ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   250
			return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   251
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   252
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
		$hash = $this->crypt_private($password, $stored_hash);
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   254
		if ($hash[0] === '*') {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
			$hash = crypt($password, $stored_hash);
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   256
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   258
		# This is not constant-time.  In order to keep the code simple,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   259
		# for timing safety we currently rely on the salts being
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   260
		# unpredictable, which they are at least in the non-fallback
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   261
		# cases (that is, when we use /dev/urandom and bcrypt).
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
		return $hash === $stored_hash;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
	}
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   264
}