passwordUtil.php 914 B

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