passwordUtil.php 894 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. class FreshRSS_password_Util {
  3. // Will also have to be computed client side on mobile devices,
  4. // so do not use a too high cost
  5. public const BCRYPT_COST = 9;
  6. /**
  7. * Return a hash of a plain password, using BCRYPT
  8. */
  9. public static function hash(string $passwordPlain): string {
  10. $passwordHash = password_hash(
  11. $passwordPlain,
  12. PASSWORD_BCRYPT,
  13. array('cost' => self::BCRYPT_COST)
  14. );
  15. // Compatibility with bcrypt.js
  16. $passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash);
  17. if ($passwordHash === '' || $passwordHash === null) {
  18. return '';
  19. }
  20. return $passwordHash;
  21. }
  22. /**
  23. * Verify the given password is valid.
  24. *
  25. * A valid password is a string of at least 7 characters.
  26. *
  27. * @return bool True if the password is valid, false otherwise
  28. */
  29. public static function check(string $password): bool {
  30. return strlen($password) >= 7;
  31. }
  32. }