PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/PasswordLib/Password/Implementation/Hash.php

http://github.com/ircmaxell/PHP-PasswordLib
PHP | 113 lines | 44 code | 9 blank | 60 comment | 2 complexity | 0cf49ad4bdd02673a851b33ed17cf718 MD5 | raw file
  1. <?php
  2. /**
  3. * The basic Hash implementation.
  4. *
  5. * It's worth noting, since there's no prefix, you cannot create a hash using
  6. * the factory method.
  7. *
  8. * PHP version 5.3
  9. *
  10. * @category PHPPasswordLib
  11. * @package Password
  12. * @subpackage Implementation
  13. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  14. * @copyright 2011 The Authors
  15. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  16. * @version Build @@version@@
  17. */
  18. namespace PasswordLib\Password\Implementation;
  19. use PasswordLib\Random\Factory as RandomFactory;
  20. /**
  21. * The basic Hash implementation.
  22. *
  23. * It's worth noting, since there's no prefix, you cannot create a hash using
  24. * the factory method.
  25. *
  26. * @category PHPPasswordLib
  27. * @package Password
  28. * @subpackage Implementation
  29. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  30. */
  31. class Hash extends \PasswordLib\Password\AbstractPassword {
  32. /**
  33. * @var Hash The hash function to use (MD5)
  34. */
  35. protected $defaultOptions = array(
  36. 'hash' => 'sha512',
  37. );
  38. /**
  39. * Determine if the hash was made with this method
  40. *
  41. * @param string $hash The hashed data to check
  42. *
  43. * @return boolean Was the hash created by this method
  44. */
  45. public static function detect($hash) {
  46. $res = preg_match('/^[a-fA-F0-9]+$/', $hash);
  47. $res &= (int) in_array(strlen($hash), array(32, 40, 64, 128));
  48. return (boolean) $res;
  49. }
  50. /**
  51. * Load an instance of the class based upon the supplied hash
  52. *
  53. * @param string $hash The hash to load from
  54. *
  55. * @return Password the created instance
  56. * @throws InvalidArgumentException if the hash wasn't created here
  57. */
  58. public static function loadFromHash($hash) {
  59. if (!static::detect($hash)) {
  60. throw new \InvalidArgumentException('Hash Not Created Here');
  61. }
  62. $hashMethod = '';
  63. switch (strlen($hash)) {
  64. case 32:
  65. $hashMethod = 'md5';
  66. break;
  67. case 40:
  68. $hashMethod = 'sha1';
  69. break;
  70. case 64:
  71. $hashMethod = 'sha256';
  72. break;
  73. case 128:
  74. $hashMethod = 'sha512';
  75. break;
  76. }
  77. return new static(array('hash' => $hashMethod));
  78. }
  79. /**
  80. * Create a password hash for a given plain text password
  81. *
  82. * @param string $password The password to hash
  83. *
  84. * @return string The formatted password hash
  85. */
  86. public function create($password) {
  87. throw new \BadMethodCallException(
  88. 'Unsalted Passwords are only implemented for verification'
  89. );
  90. }
  91. /**
  92. * Verify a password hash against a given plain text password
  93. *
  94. * @param string $password The password to hash
  95. * @param string $hash The supplied ahsh to validate
  96. *
  97. * @return boolean Does the password validate against the hash
  98. */
  99. public function verify($password, $hash) {
  100. $password = $this->checkPassword($password);
  101. $test = hash($this->options['hash'], $password);
  102. return $this->compareStrings($test, $hash);
  103. }
  104. }