]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/phpass/PasswordHash.php
Update phpass
[Github/YOURLS.git] / includes / phpass / PasswordHash.php
1 <?php
2
3 namespace Hautelook\Phpass;
4
5 /**
6  *
7  * Portable PHP password hashing framework.
8  *
9  * Version 1.0.0 - modified by Nordstromrack.com | HauteLook
10  *
11  * Change Log:
12  *
13  * - the hash_equals function is now used instead of == or === to prevent
14  *   timing attacks
15  *
16  * Written by Solar Designer <solar at openwall.com> in 2004-2006 and placed in
17  *
18  * There's absolutely no warranty.
19  *
20  * The homepage URL for this framework is:
21  *
22  *      http://www.openwall.com/phpass/
23  *
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  * @author Solar Designer <solar@openwall.com>
36  */
37 class PasswordHash
38 {
39     private $itoa64;
40     private $iteration_count_log2;
41     private $portable_hashes;
42     private $random_state;
43
44     /**
45      * Constructor
46      *
47      * @param int $iteration_count_log2
48      * @param boolean $portable_hashes
49      */
50     public function __construct($iteration_count_log2, $portable_hashes)
51     {
52         $this->itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
53
54         if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31) {
55             $iteration_count_log2 = 8;
56         }
57         $this->iteration_count_log2 = $iteration_count_log2;
58
59         $this->portable_hashes = $portable_hashes;
60
61         $this->random_state = microtime();
62         if (function_exists('getmypid')) {
63             $this->random_state .= getmypid();
64         }
65     }
66
67     /**
68      * @param  int $count
69      * @return String
70      */
71     public function get_random_bytes($count)
72     {
73         $output = '';
74         
75         if (is_callable('random_bytes')) {
76             return random_bytes($count);
77         }
78         
79         if (@is_readable('/dev/urandom') &&
80             ($fh = @fopen('/dev/urandom', 'rb'))) {
81             $output = fread($fh, $count);
82             fclose($fh);
83         }
84
85         if (strlen($output) < $count) {
86             $output = '';
87             for ($i = 0; $i < $count; $i += 16) {
88                 $this->random_state =
89                     md5(microtime() . $this->random_state);
90                 $output .=
91                     pack('H*', md5($this->random_state));
92             }
93             $output = substr($output, 0, $count);
94         }
95
96         return $output;
97     }
98
99     /**
100      * @param  String $input
101      * @param  int $count
102      * @return String
103      */
104     public function encode64($input, $count)
105     {
106         $output = '';
107         $i = 0;
108         do {
109             $value = ord($input[$i++]);
110             $output .= $this->itoa64[$value & 0x3f];
111             if ($i < $count) {
112                 $value |= ord($input[$i]) << 8;
113             }
114             $output .= $this->itoa64[($value >> 6) & 0x3f];
115             if ($i++ >= $count) {
116                 break;
117             }
118             if ($i < $count) {
119                 $value |= ord($input[$i]) << 16;
120             }
121             $output .= $this->itoa64[($value >> 12) & 0x3f];
122             if ($i++ >= $count) {
123                 break;
124             }
125             $output .= $this->itoa64[($value >> 18) & 0x3f];
126         } while ($i < $count);
127
128         return $output;
129     }
130
131     /**
132      * @param  String $input
133      * @return String
134      */
135     public function gensalt_private($input)
136     {
137         $output = '$P$';
138         $output .= $this->itoa64[min($this->iteration_count_log2 +
139             ((PHP_VERSION >= '5') ? 5 : 3), 30)];
140         $output .= $this->encode64($input, 6);
141
142         return $output;
143     }
144
145     /**
146      * @param  String $password
147      * @param  String $setting
148      * @return String
149      */
150     public function crypt_private($password, $setting)
151     {
152         $output = '*0';
153         if (substr($setting, 0, 2) == $output) {
154             $output = '*1';
155         }
156
157         $id = substr($setting, 0, 3);
158         # We use "$P$", phpBB3 uses "$H$" for the same thing
159         if ($id != '$P$' && $id != '$H$') {
160             return $output;
161         }
162
163         $count_log2 = strpos($this->itoa64, $setting[3]);
164         if ($count_log2 < 7 || $count_log2 > 30) {
165             return $output;
166         }
167
168         $count = 1 << $count_log2;
169
170         $salt = substr($setting, 4, 8);
171         if (strlen($salt) != 8) {
172             return $output;
173         }
174
175         // We're kind of forced to use MD5 here since it's the only
176         // cryptographic primitive available in all versions of PHP
177         // currently in use.  To implement our own low-level crypto
178         // in PHP would result in much worse performance and
179         // consequently in lower iteration counts and hashes that are
180         // quicker to crack (by non-PHP code).
181         if (PHP_VERSION >= '5') {
182             $hash = md5($salt . $password, TRUE);
183             do {
184                 $hash = md5($hash . $password, TRUE);
185             } while (--$count);
186         } else {
187             $hash = pack('H*', md5($salt . $password));
188             do {
189                 $hash = pack('H*', md5($hash . $password));
190             } while (--$count);
191         }
192
193         $output = substr($setting, 0, 12);
194         $output .= $this->encode64($hash, 16);
195
196         return $output;
197     }
198
199     /**
200      * @param  String $input
201      * @return String
202      */
203     public function gensalt_extended($input)
204     {
205         $count_log2 = min($this->iteration_count_log2 + 8, 24);
206         // This should be odd to not reveal weak DES keys, and the
207         // maximum valid value is (2**24 - 1) which is odd anyway.
208         $count = (1 << $count_log2) - 1;
209
210         $output = '_';
211         $output .= $this->itoa64[$count & 0x3f];
212         $output .= $this->itoa64[($count >> 6) & 0x3f];
213         $output .= $this->itoa64[($count >> 12) & 0x3f];
214         $output .= $this->itoa64[($count >> 18) & 0x3f];
215
216         $output .= $this->encode64($input, 3);
217
218         return $output;
219     }
220
221     /**
222      * @param  String $input
223      * @return String
224      */
225     public function gensalt_blowfish($input)
226     {
227         // This one needs to use a different order of characters and a
228         // different encoding scheme from the one in encode64() above.
229         // We care because the last character in our encoded string will
230         // only represent 2 bits.  While two known implementations of
231         // bcrypt will happily accept and correct a salt string which
232         // has the 4 unused bits set to non-zero, we do not want to take
233         // chances and we also do not want to waste an additional byte
234         // of entropy.
235         $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
236
237         $output = '$2a$';
238         $output .= chr(ord('0') + $this->iteration_count_log2 / 10);
239         $output .= chr(ord('0') + $this->iteration_count_log2 % 10);
240         $output .= '$';
241
242         $i = 0;
243         do {
244             $c1 = ord($input[$i++]);
245             $output .= $itoa64[$c1 >> 2];
246             $c1 = ($c1 & 0x03) << 4;
247             if ($i >= 16) {
248                 $output .= $itoa64[$c1];
249                 break;
250             }
251
252             $c2 = ord($input[$i++]);
253             $c1 |= $c2 >> 4;
254             $output .= $itoa64[$c1];
255             $c1 = ($c2 & 0x0f) << 2;
256
257             $c2 = ord($input[$i++]);
258             $c1 |= $c2 >> 6;
259             $output .= $itoa64[$c1];
260             $output .= $itoa64[$c2 & 0x3f];
261         } while (1);
262
263         return $output;
264     }
265
266     /**
267      * @param String $password
268      */
269     public function HashPassword($password)
270     {
271         $random = '';
272
273         if (CRYPT_BLOWFISH == 1 && !$this->portable_hashes) {
274             $random = $this->get_random_bytes(16);
275             $hash =
276                 crypt($password, $this->gensalt_blowfish($random));
277             if (strlen($hash) == 60) {
278                 return $hash;
279             }
280         }
281
282         if (CRYPT_EXT_DES == 1 && !$this->portable_hashes) {
283             if (strlen($random) < 3) {
284                 $random = $this->get_random_bytes(3);
285             }
286             $hash =
287                 crypt($password, $this->gensalt_extended($random));
288             if (strlen($hash) == 20) {
289                 return $hash;
290             }
291         }
292
293         if (strlen($random) < 6) {
294             $random = $this->get_random_bytes(6);
295         }
296
297         $hash =
298             $this->crypt_private($password,
299             $this->gensalt_private($random));
300         if (strlen($hash) == 34) {
301             return $hash;
302         }
303
304         // Returning '*' on error is safe here, but would _not_ be safe
305         // in a crypt(3)-like function used _both_ for generating new
306         // hashes and for validating passwords against existing hashes.
307         return '*';
308     }
309
310     /**
311      * @param String $password
312      * @param String $stored_hash
313      * @return boolean
314      */
315     public function CheckPassword($password, $stored_hash)
316     {
317         $hash = $this->crypt_private($password, $stored_hash);
318         if ($hash[0] == '*') {
319             $hash = crypt($password, $stored_hash);
320         }
321
322         return hash_equals($stored_hash, $hash);
323     }
324 }