PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/CryptLib/Password/Implementation/Hash.php

http://github.com/ircmaxell/PHP-CryptLib
PHP | 145 lines | 56 code | 12 blank | 77 comment | 4 complexity | 9616e0c041e4ac12af8b6b32f59ce902 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 PHPCryptLib
  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 CryptLib\Password\Implementation;
  19. use CryptLib\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 PHPCryptLib
  27. * @package Password
  28. * @subpackage Implementation
  29. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  30. */
  31. class Hash implements \CryptLib\Password\Password {
  32. /**
  33. * @var Generator The random generator to use for seeds
  34. */
  35. protected $generator = null;
  36. /**
  37. * @var Hash The hash function to use (MD5)
  38. */
  39. protected $hash = null;
  40. /**
  41. * Determine if the hash was made with this method
  42. *
  43. * @param string $hash The hashed data to check
  44. *
  45. * @return boolean Was the hash created by this method
  46. */
  47. public static function detect($hash) {
  48. $res = preg_match('/^[a-fA-F0-9]+$/', $hash);
  49. $res &= (int) in_array(strlen($hash), array(32, 40, 64, 128));
  50. return (boolean) $res;
  51. }
  52. /**
  53. * Return the prefix used by this hashing method
  54. *
  55. * @return string The prefix used
  56. */
  57. public static function getPrefix() {
  58. return false;
  59. }
  60. /**
  61. * Load an instance of the class based upon the supplied hash
  62. *
  63. * @param string $hash The hash to load from
  64. *
  65. * @return Password the created instance
  66. * @throws InvalidArgumentException if the hash wasn't created here
  67. */
  68. public static function loadFromHash($hash) {
  69. if (!static::detect($hash)) {
  70. throw new \InvalidArgumentException('Hash Not Created Here');
  71. }
  72. $hashMethod = '';
  73. switch (strlen($hash)) {
  74. case 32:
  75. $hashMethod = 'md5';
  76. break;
  77. case 40:
  78. $hashMethod = 'sha1';
  79. break;
  80. case 64:
  81. $hashMethod = 'sha256';
  82. break;
  83. case 128:
  84. $hashMethod = 'sha512';
  85. break;
  86. }
  87. return new static($hashMethod);
  88. }
  89. /**
  90. * Build a new instance
  91. *
  92. * @param string $hashMethod The hash function to use for hashing
  93. * @param Generator $generator The random generator to use for seeds
  94. * @param Factory $factory The hash factory to use for this instance
  95. *
  96. * @return void
  97. */
  98. public function __construct(
  99. $hashMethod,
  100. \CryptLib\Random\Generator $generator = null
  101. ) {
  102. $this->hash = $hashMethod;
  103. if (is_null($generator)) {
  104. $random = new RandomFactory();
  105. $generator = $random->getMediumStrengthGenerator();
  106. }
  107. $this->generator = $generator;
  108. }
  109. /**
  110. * Create a password hash for a given plain text password
  111. *
  112. * @param string $password The password to hash
  113. *
  114. * @return string The formatted password hash
  115. */
  116. public function create($password) {
  117. throw new \BadMethodCallException(
  118. 'Unsalted Passwords are only implemented for verification'
  119. );
  120. }
  121. /**
  122. * Verify a password hash against a given plain text password
  123. *
  124. * @param string $password The password to hash
  125. * @param string $hash The supplied ahsh to validate
  126. *
  127. * @return boolean Does the password validate against the hash
  128. */
  129. public function verify($password, $hash) {
  130. $test = hash($this->hash, $password);
  131. return $test == $hash;
  132. }
  133. }