modnative.class.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * This file contains a modHash implementation of PHP's native password_hash/password_verify handling.
  4. * @package modx
  5. * @subpackage hashing
  6. */
  7. if (!function_exists('password_hash')) {
  8. require MODX_CORE_PATH . 'model/lib/password.php';
  9. }
  10. /**
  11. * A PHP native password_hash/password_verify implementation of modHash.
  12. *
  13. * {@inheritdoc}
  14. *
  15. * @package modx
  16. * @subpackage hashing
  17. */
  18. class modNative extends modHash {
  19. /**
  20. * Generates the hash for the provided string using PHP's password_hash function.
  21. *
  22. * @param string $string A string to generate a secure hash from.
  23. * @param array $options An array of options to be passed to the hash implementation.
  24. * @return mixed The hash result or false on failure.
  25. */
  26. public function hash($string, array $options = array()) {
  27. return password_hash($string, PASSWORD_DEFAULT);
  28. }
  29. /**
  30. * Verifies with PHP's native password_verify function that the provided hash in $expected matches the
  31. * raw (unhashed) $string.
  32. *
  33. * @param string $string
  34. * @param string $expected
  35. * @param array $options
  36. * @return bool
  37. */
  38. public function verify($string, $expected, array $options = array())
  39. {
  40. return password_verify($string, $expected);
  41. }
  42. }