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