author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* Portable PHP password hashing framework. |
|
4 |
* @package phpass |
|
5 | 5 |
* @since 2.5.0 |
18 | 6 |
* @version 0.5 / WordPress |
16 | 7 |
* @link https://www.openwall.com/phpass/ |
0 | 8 |
*/ |
9 |
||
10 |
# |
|
18 | 11 |
# Portable PHP password hashing framework. |
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 | 14 |
# |
0 | 15 |
# Written by Solar Designer <solar at openwall.com> in 2004-2006 and placed in |
16 |
# the public domain. Revised in subsequent years, still public domain. |
|
17 |
# |
|
18 |
# There's absolutely no warranty. |
|
19 |
# |
|
18 | 20 |
# The homepage URL for this framework is: |
21 |
# |
|
22 |
# http://www.openwall.com/phpass/ |
|
23 |
# |
|
0 | 24 |
# Please be sure to update the Version line if you edit this file in any way. |
25 |
# It is suggested that you leave the main version number intact, but indicate |
|
26 |
# your project name (after the slash) and add your own revision information. |
|
27 |
# |
|
28 |
# Please do not change the "private" password hashing method implemented in |
|
29 |
# here, thereby making your hashes incompatible. However, if you must, please |
|
30 |
# change the hash type identifier (the "$P$") to something different. |
|
31 |
# |
|
32 |
# Obviously, since this code is in the public domain, the above are not |
|
33 |
# requirements (there can be none), but merely suggestions. |
|
34 |
# |
|
35 |
||
36 |
/** |
|
37 |
* Portable PHP password hashing framework. |
|
38 |
* |
|
39 |
* @package phpass |
|
18 | 40 |
* @version 0.5 / WordPress |
16 | 41 |
* @link https://www.openwall.com/phpass/ |
5 | 42 |
* @since 2.5.0 |
0 | 43 |
*/ |
44 |
class PasswordHash { |
|
45 |
var $itoa64; |
|
46 |
var $iteration_count_log2; |
|
47 |
var $portable_hashes; |
|
48 |
var $random_state; |
|
49 |
||
18 | 50 |
function __construct($iteration_count_log2, $portable_hashes) |
0 | 51 |
{ |
52 |
$this->itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; |
|
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 | 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 | 57 |
$this->iteration_count_log2 = $iteration_count_log2; |
58 |
||
59 |
$this->portable_hashes = $portable_hashes; |
|
60 |
||
18 | 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 | 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 | 65 |
} |
66 |
||
18 | 67 |
function PasswordHash($iteration_count_log2, $portable_hashes) |
68 |
{ |
|
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 | 72 |
function get_random_bytes($count) |
73 |
{ |
|
74 |
$output = ''; |
|
18 | 75 |
if (@is_readable('/dev/urandom') && |
0 | 76 |
($fh = @fopen('/dev/urandom', 'rb'))) { |
77 |
$output = fread($fh, $count); |
|
78 |
fclose($fh); |
|
79 |
} |
|
80 |
||
81 |
if (strlen($output) < $count) { |
|
82 |
$output = ''; |
|
83 |
for ($i = 0; $i < $count; $i += 16) { |
|
84 |
$this->random_state = |
|
85 |
md5(microtime() . $this->random_state); |
|
18 | 86 |
$output .= md5($this->random_state, TRUE); |
0 | 87 |
} |
88 |
$output = substr($output, 0, $count); |
|
89 |
} |
|
90 |
||
91 |
return $output; |
|
92 |
} |
|
93 |
||
94 |
function encode64($input, $count) |
|
95 |
{ |
|
96 |
$output = ''; |
|
97 |
$i = 0; |
|
98 |
do { |
|
99 |
$value = ord($input[$i++]); |
|
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 | 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 | 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 | 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 | 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 | 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 | 113 |
break; |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
114 |
} |
0 | 115 |
$output .= $this->itoa64[($value >> 18) & 0x3f]; |
116 |
} while ($i < $count); |
|
117 |
||
118 |
return $output; |
|
119 |
} |
|
120 |
||
121 |
function gensalt_private($input) |
|
122 |
{ |
|
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 | 126 |
$output .= $this->encode64($input, 6); |
127 |
||
128 |
return $output; |
|
129 |
} |
|
130 |
||
131 |
function crypt_private($password, $setting) |
|
132 |
{ |
|
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 | 135 |
$output = '*1'; |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
136 |
} |
0 | 137 |
|
138 |
$id = substr($setting, 0, 3); |
|
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 | 141 |
return $output; |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
142 |
} |
0 | 143 |
|
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 | 146 |
return $output; |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
147 |
} |
0 | 148 |
|
149 |
$count = 1 << $count_log2; |
|
150 |
||
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 | 153 |
return $output; |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
154 |
} |
0 | 155 |
|
18 | 156 |
# We were kind of forced to use MD5 here since it's the only |
157 |
# cryptographic primitive that was available in all versions |
|
158 |
# of PHP in use. To implement our own low-level crypto in PHP |
|
159 |
# would have resulted in much worse performance and |
|
0 | 160 |
# consequently in lower iteration counts and hashes that are |
161 |
# quicker to crack (by non-PHP code). |
|
18 | 162 |
$hash = md5($salt . $password, TRUE); |
163 |
do { |
|
164 |
$hash = md5($hash . $password, TRUE); |
|
165 |
} while (--$count); |
|
0 | 166 |
|
167 |
$output = substr($setting, 0, 12); |
|
168 |
$output .= $this->encode64($hash, 16); |
|
169 |
||
170 |
return $output; |
|
171 |
} |
|
172 |
||
173 |
function gensalt_blowfish($input) |
|
174 |
{ |
|
175 |
# This one needs to use a different order of characters and a |
|
176 |
# different encoding scheme from the one in encode64() above. |
|
177 |
# We care because the last character in our encoded string will |
|
178 |
# only represent 2 bits. While two known implementations of |
|
179 |
# bcrypt will happily accept and correct a salt string which |
|
180 |
# has the 4 unused bits set to non-zero, we do not want to take |
|
181 |
# chances and we also do not want to waste an additional byte |
|
182 |
# of entropy. |
|
183 |
$itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
|
184 |
||
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 | 188 |
$output .= '$'; |
189 |
||
190 |
$i = 0; |
|
191 |
do { |
|
192 |
$c1 = ord($input[$i++]); |
|
193 |
$output .= $itoa64[$c1 >> 2]; |
|
194 |
$c1 = ($c1 & 0x03) << 4; |
|
195 |
if ($i >= 16) { |
|
196 |
$output .= $itoa64[$c1]; |
|
197 |
break; |
|
198 |
} |
|
199 |
||
200 |
$c2 = ord($input[$i++]); |
|
201 |
$c1 |= $c2 >> 4; |
|
202 |
$output .= $itoa64[$c1]; |
|
203 |
$c1 = ($c2 & 0x0f) << 2; |
|
204 |
||
205 |
$c2 = ord($input[$i++]); |
|
206 |
$c1 |= $c2 >> 6; |
|
207 |
$output .= $itoa64[$c1]; |
|
208 |
$output .= $itoa64[$c2 & 0x3f]; |
|
209 |
} while (1); |
|
210 |
||
211 |
return $output; |
|
212 |
} |
|
213 |
||
214 |
function HashPassword($password) |
|
215 |
{ |
|
5 | 216 |
if ( strlen( $password ) > 4096 ) { |
217 |
return '*'; |
|
218 |
} |
|
219 |
||
0 | 220 |
$random = ''; |
221 |
||
18 | 222 |
if (CRYPT_BLOWFISH === 1 && !$this->portable_hashes) { |
0 | 223 |
$random = $this->get_random_bytes(16); |
224 |
$hash = |
|
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 | 227 |
return $hash; |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
228 |
} |
0 | 229 |
} |
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 | 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 | 234 |
$hash = |
235 |
$this->crypt_private($password, |
|
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 | 238 |
return $hash; |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
239 |
} |
0 | 240 |
|
241 |
# Returning '*' on error is safe here, but would _not_ be safe |
|
242 |
# in a crypt(3)-like function used _both_ for generating new |
|
243 |
# hashes and for validating passwords against existing hashes. |
|
244 |
return '*'; |
|
245 |
} |
|
246 |
||
247 |
function CheckPassword($password, $stored_hash) |
|
248 |
{ |
|
5 | 249 |
if ( strlen( $password ) > 4096 ) { |
250 |
return false; |
|
251 |
} |
|
252 |
||
0 | 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 | 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 | 257 |
|
18 | 258 |
# This is not constant-time. In order to keep the code simple, |
259 |
# for timing safety we currently rely on the salts being |
|
260 |
# unpredictable, which they are at least in the non-fallback |
|
261 |
# cases (that is, when we use /dev/urandom and bcrypt). |
|
0 | 262 |
return $hash === $stored_hash; |
263 |
} |
|
18 | 264 |
} |