Complex classes like PassHash often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PassHash, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class PassHash { |
||
15 | /** |
||
16 | * Verifies a cleartext password against a crypted hash |
||
17 | * |
||
18 | * The method and salt used for the crypted hash is determined automatically, |
||
19 | * then the clear text password is crypted using the same method. If both hashs |
||
20 | * match true is is returned else false |
||
21 | * |
||
22 | * @author Andreas Gohr <[email protected]> |
||
23 | * |
||
24 | * @param string $clear Clear-Text password |
||
25 | * @param string $hash Hash to compare against |
||
26 | * @return bool |
||
27 | */ |
||
28 | public function verify_hash($clear, $hash) { |
||
104 | |||
105 | /** |
||
106 | * Create a random salt |
||
107 | * |
||
108 | * @param int $len The length of the salt |
||
109 | * @return string |
||
110 | */ |
||
111 | public function gen_salt($len = 32) { |
||
119 | |||
120 | /** |
||
121 | * Initialize the passed variable with a salt if needed. |
||
122 | * |
||
123 | * If $salt is not null, the value is kept, but the lenght restriction is |
||
124 | * applied (unless, $cut is false). |
||
125 | * |
||
126 | * @param string|null &$salt The salt, pass null if you want one generated |
||
127 | * @param int $len The length of the salt |
||
128 | * @param bool $cut Apply length restriction to existing salt? |
||
129 | */ |
||
130 | public function init_salt(&$salt, $len = 32, $cut = true) { |
||
137 | |||
138 | // Password hashing methods follow below |
||
139 | |||
140 | /** |
||
141 | * Password hashing method 'smd5' |
||
142 | * |
||
143 | * Uses salted MD5 hashs. Salt is 8 bytes long. |
||
144 | * |
||
145 | * The same mechanism is used by Apache's 'apr1' method. This will |
||
146 | * fallback to a implementation in pure PHP if MD5 support is not |
||
147 | * available in crypt() |
||
148 | * |
||
149 | * @author Andreas Gohr <[email protected]> |
||
150 | * @author <mikey_nich at hotmail dot com> |
||
151 | * @link http://php.net/manual/en/function.crypt.php#73619 |
||
152 | * |
||
153 | * @param string $clear The clear text to hash |
||
154 | * @param string $salt The salt to use, null for random |
||
155 | * @return string Hashed password |
||
156 | */ |
||
157 | public function hash_smd5($clear, $salt = null) { |
||
167 | |||
168 | /** |
||
169 | * Password hashing method 'lsmd5' |
||
170 | * |
||
171 | * Uses salted MD5 hashs. Salt is 8 bytes long. |
||
172 | * |
||
173 | * This is the format used by LDAP. |
||
174 | * |
||
175 | * @param string $clear The clear text to hash |
||
176 | * @param string $salt The salt to use, null for random |
||
177 | * @return string Hashed password |
||
178 | */ |
||
179 | public function hash_lsmd5($clear, $salt = null) { |
||
183 | |||
184 | /** |
||
185 | * Password hashing method 'apr1' |
||
186 | * |
||
187 | * Uses salted MD5 hashs. Salt is 8 bytes long. |
||
188 | * |
||
189 | * This is basically the same as smd1 above, but as used by Apache. |
||
190 | * |
||
191 | * @author <mikey_nich at hotmail dot com> |
||
192 | * @link http://php.net/manual/en/function.crypt.php#73619 |
||
193 | * |
||
194 | * @param string $clear The clear text to hash |
||
195 | * @param string $salt The salt to use, null for random |
||
196 | * @param string $magic The hash identifier (apr1 or 1) |
||
197 | * @return string Hashed password |
||
198 | */ |
||
199 | public function hash_apr1($clear, $salt = null, $magic = 'apr1') { |
||
234 | |||
235 | /** |
||
236 | * Password hashing method 'md5' |
||
237 | * |
||
238 | * Uses MD5 hashs. |
||
239 | * |
||
240 | * @param string $clear The clear text to hash |
||
241 | * @return string Hashed password |
||
242 | */ |
||
243 | public function hash_md5($clear) { |
||
246 | |||
247 | /** |
||
248 | * Password hashing method 'sha1' |
||
249 | * |
||
250 | * Uses SHA1 hashs. |
||
251 | * |
||
252 | * @param string $clear The clear text to hash |
||
253 | * @return string Hashed password |
||
254 | */ |
||
255 | public function hash_sha1($clear) { |
||
258 | |||
259 | /** |
||
260 | * Password hashing method 'ssha' as used by LDAP |
||
261 | * |
||
262 | * Uses salted SHA1 hashs. Salt is 4 bytes long. |
||
263 | * |
||
264 | * @param string $clear The clear text to hash |
||
265 | * @param string $salt The salt to use, null for random |
||
266 | * @return string Hashed password |
||
267 | */ |
||
268 | public function hash_ssha($clear, $salt = null) { |
||
272 | |||
273 | /** |
||
274 | * Password hashing method 'crypt' |
||
275 | * |
||
276 | * Uses salted crypt hashs. Salt is 2 bytes long. |
||
277 | * |
||
278 | * @param string $clear The clear text to hash |
||
279 | * @param string $salt The salt to use, null for random |
||
280 | * @return string Hashed password |
||
281 | */ |
||
282 | public function hash_crypt($clear, $salt = null) { |
||
286 | |||
287 | /** |
||
288 | * Password hashing method 'mysql' |
||
289 | * |
||
290 | * This method was used by old MySQL systems |
||
291 | * |
||
292 | * @link http://php.net/mysql |
||
293 | * @author <soren at byu dot edu> |
||
294 | * @param string $clear The clear text to hash |
||
295 | * @return string Hashed password |
||
296 | */ |
||
297 | public function hash_mysql($clear) { |
||
311 | |||
312 | /** |
||
313 | * Password hashing method 'my411' |
||
314 | * |
||
315 | * Uses SHA1 hashs. This method is used by MySQL 4.11 and above |
||
316 | * |
||
317 | * @param string $clear The clear text to hash |
||
318 | * @return string Hashed password |
||
319 | */ |
||
320 | public function hash_my411($clear) { |
||
323 | |||
324 | /** |
||
325 | * Password hashing method 'kmd5' |
||
326 | * |
||
327 | * Uses salted MD5 hashs. |
||
328 | * |
||
329 | * Salt is 2 bytes long, but stored at position 16, so you need to pass at |
||
330 | * least 18 bytes. You can pass the crypted hash as salt. |
||
331 | * |
||
332 | * @param string $clear The clear text to hash |
||
333 | * @param string $salt The salt to use, null for random |
||
334 | * @return string Hashed password |
||
335 | */ |
||
336 | public function hash_kmd5($clear, $salt = null) { |
||
344 | |||
345 | /** |
||
346 | * Password hashing method 'pmd5' |
||
347 | * |
||
348 | * Uses salted MD5 hashs. Salt is 1+8 bytes long, 1st byte is the |
||
349 | * iteration count when given, for null salts $compute is used. |
||
350 | * |
||
351 | * The actual iteration count is the given count squared, maximum is |
||
352 | * 30 (-> 1073741824). If a higher one is given, the function throws |
||
353 | * an exception. |
||
354 | * |
||
355 | * @link http://www.openwall.com/phpass/ |
||
356 | * |
||
357 | * @param string $clear The clear text to hash |
||
358 | * @param string $salt The salt to use, null for random |
||
359 | * @param string $magic The hash identifier (P or H) |
||
360 | * @param int $compute The iteration count for new passwords |
||
361 | * @throws \Exception |
||
362 | * @return string Hashed password |
||
363 | */ |
||
364 | public function hash_pmd5($clear, $salt = null, $magic = 'P', $compute = 8) { |
||
409 | |||
410 | /** |
||
411 | * Alias for hash_pmd5 |
||
412 | * |
||
413 | * @param string $clear |
||
414 | * @param null|string $salt |
||
415 | * @param string $magic |
||
416 | * @param int $compute |
||
417 | * |
||
418 | * @return string |
||
419 | * @throws \Exception |
||
420 | */ |
||
421 | public function hash_hmd5($clear, $salt = null, $magic = 'H', $compute = 8) { |
||
424 | |||
425 | /** |
||
426 | * Password hashing method 'djangosha1' |
||
427 | * |
||
428 | * Uses salted SHA1 hashs. Salt is 5 bytes long. |
||
429 | * This is used by the Django Python framework |
||
430 | * |
||
431 | * @link http://docs.djangoproject.com/en/dev/topics/auth/#passwords |
||
432 | * |
||
433 | * @param string $clear The clear text to hash |
||
434 | * @param string $salt The salt to use, null for random |
||
435 | * @return string Hashed password |
||
436 | */ |
||
437 | public function hash_djangosha1($clear, $salt = null) { |
||
441 | |||
442 | /** |
||
443 | * Password hashing method 'djangomd5' |
||
444 | * |
||
445 | * Uses salted MD5 hashs. Salt is 5 bytes long. |
||
446 | * This is used by the Django Python framework |
||
447 | * |
||
448 | * @link http://docs.djangoproject.com/en/dev/topics/auth/#passwords |
||
449 | * |
||
450 | * @param string $clear The clear text to hash |
||
451 | * @param string $salt The salt to use, null for random |
||
452 | * @return string Hashed password |
||
453 | */ |
||
454 | public function hash_djangomd5($clear, $salt = null) { |
||
458 | |||
459 | /** |
||
460 | * Password hashing method 'djangopbkdf2' |
||
461 | * |
||
462 | * An algorithm and iteration count should be given in the opts array. |
||
463 | * Defaults to sha256 and 24000 iterations |
||
464 | * |
||
465 | * @param string $clear The clear text to hash |
||
466 | * @param string $salt The salt to use, null for random |
||
467 | * @param array $opts ('algo' => hash algorithm, 'iter' => iterations) |
||
468 | * @return string Hashed password |
||
469 | * @throws \Exception when PHP is missing support for the method/algo |
||
470 | */ |
||
471 | public function hash_djangopbkdf2($clear, $salt=null, $opts=array()) { |
||
493 | |||
494 | /** |
||
495 | * Alias for djangopbkdf2 defaulting to sha256 as hash algorithm |
||
496 | * |
||
497 | * @param string $clear The clear text to hash |
||
498 | * @param string $salt The salt to use, null for random |
||
499 | * @param array $opts ('iter' => iterations) |
||
500 | * @return string Hashed password |
||
501 | * @throws \Exception when PHP is missing support for the method/algo |
||
502 | */ |
||
503 | public function hash_djangopbkdf2_sha256($clear, $salt=null, $opts=array()) { |
||
507 | |||
508 | /** |
||
509 | * Alias for djangopbkdf2 defaulting to sha1 as hash algorithm |
||
510 | * |
||
511 | * @param string $clear The clear text to hash |
||
512 | * @param string $salt The salt to use, null for random |
||
513 | * @param array $opts ('iter' => iterations) |
||
514 | * @return string Hashed password |
||
515 | * @throws \Exception when PHP is missing support for the method/algo |
||
516 | */ |
||
517 | public function hash_djangopbkdf2_sha1($clear, $salt=null, $opts=array()) { |
||
521 | |||
522 | /** |
||
523 | * Passwordhashing method 'bcrypt' |
||
524 | * |
||
525 | * Uses a modified blowfish algorithm called eksblowfish |
||
526 | * This method works on PHP 5.3+ only and will throw an exception |
||
527 | * if the needed crypt support isn't available |
||
528 | * |
||
529 | * A full hash should be given as salt (starting with $a2$) or this |
||
530 | * will break. When no salt is given, the iteration count can be set |
||
531 | * through the $compute variable. |
||
532 | * |
||
533 | * @param string $clear The clear text to hash |
||
534 | * @param string $salt The salt to use, null for random |
||
535 | * @param int $compute The iteration count (between 4 and 31) |
||
536 | * @throws \Exception |
||
537 | * @return string Hashed password |
||
538 | */ |
||
539 | public function hash_bcrypt($clear, $salt = null, $compute = 10) { |
||
552 | |||
553 | /** |
||
554 | * Password hashing method SHA512 |
||
555 | * |
||
556 | * This is only supported on PHP 5.3.2 or higher and will throw an exception if |
||
557 | * the needed crypt support is not available |
||
558 | * |
||
559 | * @param string $clear The clear text to hash |
||
560 | * @param string $salt The salt to use, null for random |
||
561 | * @param string $magic The rounds for sha512 (for example "rounds=3000"), null for default value |
||
562 | * @return string Hashed password |
||
563 | * @throws \Exception |
||
564 | */ |
||
565 | public function hash_sha512($clear, $salt = null, $magic = null) { |
||
576 | |||
577 | /** |
||
578 | * Password hashing method 'mediawiki' |
||
579 | * |
||
580 | * Uses salted MD5, this is referred to as Method B in MediaWiki docs. Unsalted md5 |
||
581 | * method 'A' is not supported. |
||
582 | * |
||
583 | * @link http://www.mediawiki.org/wiki/Manual_talk:User_table#user_password_column |
||
584 | * |
||
585 | * @param string $clear The clear text to hash |
||
586 | * @param string $salt The salt to use, null for random |
||
587 | * @return string Hashed password |
||
588 | */ |
||
589 | public function hash_mediawiki($clear, $salt = null) { |
||
593 | |||
594 | /** |
||
595 | * Wraps around native hash_hmac() or reimplents it |
||
596 | * |
||
597 | * This is not directly used as password hashing method, and thus isn't callable via the |
||
598 | * verify_hash() method. It should be used to create signatures and might be used in other |
||
599 | * password hashing methods. |
||
600 | * |
||
601 | * @see hash_hmac() |
||
602 | * @author KC Cloyd |
||
603 | * @link http://php.net/manual/en/function.hash-hmac.php#93440 |
||
604 | * |
||
605 | * @param string $algo Name of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", |
||
606 | * etc..) See hash_algos() for a list of supported algorithms. |
||
607 | * @param string $data Message to be hashed. |
||
608 | * @param string $key Shared secret key used for generating the HMAC variant of the message digest. |
||
609 | * @param bool $raw_output When set to TRUE, outputs raw binary data. FALSE outputs lowercase hexits. |
||
610 | * @return string |
||
611 | */ |
||
612 | public static function hmac($algo, $data, $key, $raw_output = false) { |
||
639 | |||
640 | /** |
||
641 | * Use a secure random generator |
||
642 | * |
||
643 | * @param int $min |
||
644 | * @param int $max |
||
645 | * @return int |
||
646 | */ |
||
647 | protected function random($min, $max){ |
||
656 | } |
||
657 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.