AbstractPasswordHasher.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.4.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Auth;
  16. use Cake\Core\InstanceConfigTrait;
  17. /**
  18. * Abstract password hashing class
  19. *
  20. */
  21. abstract class AbstractPasswordHasher
  22. {
  23. use InstanceConfigTrait;
  24. /**
  25. * Default config
  26. *
  27. * These are merged with user-provided config when the object is used.
  28. *
  29. * @var array
  30. */
  31. protected $_defaultConfig = [];
  32. /**
  33. * Constructor
  34. *
  35. * @param array $config Array of config.
  36. */
  37. public function __construct(array $config = [])
  38. {
  39. $this->config($config);
  40. }
  41. /**
  42. * Generates password hash.
  43. *
  44. * @param string|array $password Plain text password to hash or array of data
  45. * required to generate password hash.
  46. * @return string Password hash
  47. */
  48. abstract public function hash($password);
  49. /**
  50. * Check hash. Generate hash from user provided password string or data array
  51. * and check against existing hash.
  52. *
  53. * @param string|array $password Plain text password to hash or data array.
  54. * @param string $hashedPassword Existing hashed password.
  55. * @return bool True if hashes match else false.
  56. */
  57. abstract public function check($password, $hashedPassword);
  58. /**
  59. * Returns true if the password need to be rehashed, due to the password being
  60. * created with anything else than the passwords generated by this class.
  61. *
  62. * Returns true by default since the only implementation users should rely
  63. * on is the one provided by default in php 5.5+ or any compatible library
  64. *
  65. * @param string $password The password to verify
  66. * @return bool
  67. */
  68. public function needsRehash($password)
  69. {
  70. return password_needs_rehash($password, PASSWORD_DEFAULT);
  71. }
  72. }