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 |
||
| 10 | class PassHash { |
||
| 11 | /** |
||
| 12 | * Verifies a cleartext password against a crypted hash |
||
| 13 | * |
||
| 14 | * The method and salt used for the crypted hash is determined automatically, |
||
| 15 | * then the clear text password is crypted using the same method. If both hashs |
||
| 16 | * match true is is returned else false |
||
| 17 | * |
||
| 18 | * @author Andreas Gohr <[email protected]> |
||
| 19 | * |
||
| 20 | * @param string $clear Clear-Text password |
||
| 21 | * @param string $hash Hash to compare against |
||
| 22 | * @return bool |
||
| 23 | */ |
||
| 24 | function verify_hash($clear, $hash) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Create a random salt |
||
| 102 | * |
||
| 103 | * @param int $len The length of the salt |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | public function gen_salt($len = 32) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Initialize the passed variable with a salt if needed. |
||
| 117 | * |
||
| 118 | * If $salt is not null, the value is kept, but the lenght restriction is |
||
| 119 | * applied (unless, $cut is false). |
||
| 120 | * |
||
| 121 | * @param string|null &$salt The salt, pass null if you want one generated |
||
| 122 | * @param int $len The length of the salt |
||
| 123 | * @param bool $cut Apply length restriction to existing salt? |
||
| 124 | */ |
||
| 125 | public function init_salt(&$salt, $len = 32, $cut = true) { |
||
| 132 | |||
| 133 | // Password hashing methods follow below |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Password hashing method 'smd5' |
||
| 137 | * |
||
| 138 | * Uses salted MD5 hashs. Salt is 8 bytes long. |
||
| 139 | * |
||
| 140 | * The same mechanism is used by Apache's 'apr1' method. This will |
||
| 141 | * fallback to a implementation in pure PHP if MD5 support is not |
||
| 142 | * available in crypt() |
||
| 143 | * |
||
| 144 | * @author Andreas Gohr <[email protected]> |
||
| 145 | * @author <mikey_nich at hotmail dot com> |
||
| 146 | * @link http://de.php.net/manual/en/function.crypt.php#73619 |
||
| 147 | * |
||
| 148 | * @param string $clear The clear text to hash |
||
| 149 | * @param string $salt The salt to use, null for random |
||
| 150 | * @return string Hashed password |
||
| 151 | */ |
||
| 152 | public function hash_smd5($clear, $salt = null) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Password hashing method 'lsmd5' |
||
| 165 | * |
||
| 166 | * Uses salted MD5 hashs. Salt is 8 bytes long. |
||
| 167 | * |
||
| 168 | * This is the format used by LDAP. |
||
| 169 | * |
||
| 170 | * @param string $clear The clear text to hash |
||
| 171 | * @param string $salt The salt to use, null for random |
||
| 172 | * @return string Hashed password |
||
| 173 | */ |
||
| 174 | public function hash_lsmd5($clear, $salt = null) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Password hashing method 'apr1' |
||
| 181 | * |
||
| 182 | * Uses salted MD5 hashs. Salt is 8 bytes long. |
||
| 183 | * |
||
| 184 | * This is basically the same as smd1 above, but as used by Apache. |
||
| 185 | * |
||
| 186 | * @author <mikey_nich at hotmail dot com> |
||
| 187 | * @link http://de.php.net/manual/en/function.crypt.php#73619 |
||
| 188 | * |
||
| 189 | * @param string $clear The clear text to hash |
||
| 190 | * @param string $salt The salt to use, null for random |
||
| 191 | * @param string $magic The hash identifier (apr1 or 1) |
||
| 192 | * @return string Hashed password |
||
| 193 | */ |
||
| 194 | public function hash_apr1($clear, $salt = null, $magic = 'apr1') { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Password hashing method 'md5' |
||
| 232 | * |
||
| 233 | * Uses MD5 hashs. |
||
| 234 | * |
||
| 235 | * @param string $clear The clear text to hash |
||
| 236 | * @return string Hashed password |
||
| 237 | */ |
||
| 238 | public function hash_md5($clear) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Password hashing method 'sha1' |
||
| 244 | * |
||
| 245 | * Uses SHA1 hashs. |
||
| 246 | * |
||
| 247 | * @param string $clear The clear text to hash |
||
| 248 | * @return string Hashed password |
||
| 249 | */ |
||
| 250 | public function hash_sha1($clear) { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Password hashing method 'ssha' as used by LDAP |
||
| 256 | * |
||
| 257 | * Uses salted SHA1 hashs. Salt is 4 bytes long. |
||
| 258 | * |
||
| 259 | * @param string $clear The clear text to hash |
||
| 260 | * @param string $salt The salt to use, null for random |
||
| 261 | * @return string Hashed password |
||
| 262 | */ |
||
| 263 | public function hash_ssha($clear, $salt = null) { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Password hashing method 'crypt' |
||
| 270 | * |
||
| 271 | * Uses salted crypt hashs. Salt is 2 bytes long. |
||
| 272 | * |
||
| 273 | * @param string $clear The clear text to hash |
||
| 274 | * @param string $salt The salt to use, null for random |
||
| 275 | * @return string Hashed password |
||
| 276 | */ |
||
| 277 | public function hash_crypt($clear, $salt = null) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Password hashing method 'mysql' |
||
| 284 | * |
||
| 285 | * This method was used by old MySQL systems |
||
| 286 | * |
||
| 287 | * @link http://www.php.net/mysql |
||
| 288 | * @author <soren at byu dot edu> |
||
| 289 | * @param string $clear The clear text to hash |
||
| 290 | * @return string Hashed password |
||
| 291 | */ |
||
| 292 | public function hash_mysql($clear) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Password hashing method 'my411' |
||
| 309 | * |
||
| 310 | * Uses SHA1 hashs. This method is used by MySQL 4.11 and above |
||
| 311 | * |
||
| 312 | * @param string $clear The clear text to hash |
||
| 313 | * @return string Hashed password |
||
| 314 | */ |
||
| 315 | public function hash_my411($clear) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Password hashing method 'kmd5' |
||
| 321 | * |
||
| 322 | * Uses salted MD5 hashs. |
||
| 323 | * |
||
| 324 | * Salt is 2 bytes long, but stored at position 16, so you need to pass at |
||
| 325 | * least 18 bytes. You can pass the crypted hash as salt. |
||
| 326 | * |
||
| 327 | * @param string $clear The clear text to hash |
||
| 328 | * @param string $salt The salt to use, null for random |
||
| 329 | * @return string Hashed password |
||
| 330 | */ |
||
| 331 | public function hash_kmd5($clear, $salt = null) { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Password hashing method 'pmd5' |
||
| 342 | * |
||
| 343 | * Uses salted MD5 hashs. Salt is 1+8 bytes long, 1st byte is the |
||
| 344 | * iteration count when given, for null salts $compute is used. |
||
| 345 | * |
||
| 346 | * The actual iteration count is the given count squared, maximum is |
||
| 347 | * 30 (-> 1073741824). If a higher one is given, the function throws |
||
| 348 | * an exception. |
||
| 349 | * |
||
| 350 | * @link http://www.openwall.com/phpass/ |
||
| 351 | * |
||
| 352 | * @param string $clear The clear text to hash |
||
| 353 | * @param string $salt The salt to use, null for random |
||
| 354 | * @param string $magic The hash identifier (P or H) |
||
| 355 | * @param int $compute The iteration count for new passwords |
||
| 356 | * @throws Exception |
||
| 357 | * @return string Hashed password |
||
| 358 | */ |
||
| 359 | public function hash_pmd5($clear, $salt = null, $magic = 'P', $compute = 8) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Alias for hash_pmd5 |
||
| 407 | */ |
||
| 408 | public function hash_hmd5($clear, $salt = null, $magic = 'H', $compute = 8) { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Password hashing method 'djangosha1' |
||
| 414 | * |
||
| 415 | * Uses salted SHA1 hashs. Salt is 5 bytes long. |
||
| 416 | * This is used by the Django Python framework |
||
| 417 | * |
||
| 418 | * @link http://docs.djangoproject.com/en/dev/topics/auth/#passwords |
||
| 419 | * |
||
| 420 | * @param string $clear The clear text to hash |
||
| 421 | * @param string $salt The salt to use, null for random |
||
| 422 | * @return string Hashed password |
||
| 423 | */ |
||
| 424 | public function hash_djangosha1($clear, $salt = null) { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Password hashing method 'djangomd5' |
||
| 431 | * |
||
| 432 | * Uses salted MD5 hashs. Salt is 5 bytes long. |
||
| 433 | * This is used by the Django Python framework |
||
| 434 | * |
||
| 435 | * @link http://docs.djangoproject.com/en/dev/topics/auth/#passwords |
||
| 436 | * |
||
| 437 | * @param string $clear The clear text to hash |
||
| 438 | * @param string $salt The salt to use, null for random |
||
| 439 | * @return string Hashed password |
||
| 440 | */ |
||
| 441 | public function hash_djangomd5($clear, $salt = null) { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Password hashing method 'djangopbkdf2' |
||
| 448 | * |
||
| 449 | * An algorithm and iteration count should be given in the opts array. |
||
| 450 | * Defaults to sha256 and 24000 iterations |
||
| 451 | * |
||
| 452 | * @param string $clear The clear text to hash |
||
| 453 | * @param string $salt The salt to use, null for random |
||
| 454 | * @param array $opts ('algo' => hash algorithm, 'iter' => iterations) |
||
| 455 | * @return string Hashed password |
||
| 456 | * @throws Exception when PHP is missing support for the method/algo |
||
| 457 | */ |
||
| 458 | public function hash_djangopbkdf2($clear, $salt=null, $opts=array()) { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Alias for djangopbkdf2 defaulting to sha256 as hash algorithm |
||
| 483 | * |
||
| 484 | * @param string $clear The clear text to hash |
||
| 485 | * @param string $salt The salt to use, null for random |
||
| 486 | * @param array $opts ('iter' => iterations) |
||
| 487 | * @return string Hashed password |
||
| 488 | * @throws Exception when PHP is missing support for the method/algo |
||
| 489 | */ |
||
| 490 | public function hash_djangopbkdf2_sha256($clear, $salt=null, $opts=array()) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Alias for djangopbkdf2 defaulting to sha1 as hash algorithm |
||
| 497 | * |
||
| 498 | * @param string $clear The clear text to hash |
||
| 499 | * @param string $salt The salt to use, null for random |
||
| 500 | * @param array $opts ('iter' => iterations) |
||
| 501 | * @return string Hashed password |
||
| 502 | * @throws Exception when PHP is missing support for the method/algo |
||
| 503 | */ |
||
| 504 | public function hash_djangopbkdf2_sha1($clear, $salt=null, $opts=array()) { |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Passwordhashing method 'bcrypt' |
||
| 511 | * |
||
| 512 | * Uses a modified blowfish algorithm called eksblowfish |
||
| 513 | * This method works on PHP 5.3+ only and will throw an exception |
||
| 514 | * if the needed crypt support isn't available |
||
| 515 | * |
||
| 516 | * A full hash should be given as salt (starting with $a2$) or this |
||
| 517 | * will break. When no salt is given, the iteration count can be set |
||
| 518 | * through the $compute variable. |
||
| 519 | * |
||
| 520 | * @param string $clear The clear text to hash |
||
| 521 | * @param string $salt The salt to use, null for random |
||
| 522 | * @param int $compute The iteration count (between 4 and 31) |
||
| 523 | * @throws Exception |
||
| 524 | * @return string Hashed password |
||
| 525 | */ |
||
| 526 | public function hash_bcrypt($clear, $salt = null, $compute = 8) { |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Password hashing method SHA512 |
||
| 542 | * |
||
| 543 | * This is only supported on PHP 5.3.2 or higher and will throw an exception if |
||
| 544 | * the needed crypt support is not available |
||
| 545 | * |
||
| 546 | * @param string $clear The clear text to hash |
||
| 547 | * @param string $salt The salt to use, null for random |
||
| 548 | * @return string Hashed password |
||
| 549 | * @throws Exception |
||
| 550 | */ |
||
| 551 | public function hash_sha512($clear, $salt = null) { |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Password hashing method 'mediawiki' |
||
| 561 | * |
||
| 562 | * Uses salted MD5, this is referred to as Method B in MediaWiki docs. Unsalted md5 |
||
| 563 | * method 'A' is not supported. |
||
| 564 | * |
||
| 565 | * @link http://www.mediawiki.org/wiki/Manual_talk:User_table#user_password_column |
||
| 566 | * |
||
| 567 | * @param string $clear The clear text to hash |
||
| 568 | * @param string $salt The salt to use, null for random |
||
| 569 | * @return string Hashed password |
||
| 570 | */ |
||
| 571 | public function hash_mediawiki($clear, $salt = null) { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Wraps around native hash_hmac() or reimplents it |
||
| 578 | * |
||
| 579 | * This is not directly used as password hashing method, and thus isn't callable via the |
||
| 580 | * verify_hash() method. It should be used to create signatures and might be used in other |
||
| 581 | * password hashing methods. |
||
| 582 | * |
||
| 583 | * @see hash_hmac() |
||
| 584 | * @author KC Cloyd |
||
| 585 | * @link http://www.php.net/manual/en/function.hash-hmac.php#93440 |
||
| 586 | * |
||
| 587 | * @param string $algo Name of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", |
||
| 588 | * etc..) See hash_algos() for a list of supported algorithms. |
||
| 589 | * @param string $data Message to be hashed. |
||
| 590 | * @param string $key Shared secret key used for generating the HMAC variant of the message digest. |
||
| 591 | * @param bool $raw_output When set to TRUE, outputs raw binary data. FALSE outputs lowercase hexits. |
||
| 592 | * @return string |
||
| 593 | */ |
||
| 594 | public static function hmac($algo, $data, $key, $raw_output = false) { |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Use DokuWiki's secure random generator if available |
||
| 624 | * |
||
| 625 | * @param int $min |
||
| 626 | * @param int $max |
||
| 627 | * @return int |
||
| 628 | */ |
||
| 629 | protected function random($min, $max){ |
||
| 636 | } |
||
| 637 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.