src/Security/Hasher/IdempierePasswordHasher.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Security\Hasher;
  3. use Symfony\Component\PasswordHasher\Exception\InvalidPasswordException;
  4. use Symfony\Component\PasswordHasher\Hasher\CheckPasswordLengthTrait;
  5. use Symfony\Component\PasswordHasher\LegacyPasswordHasherInterface;
  6. class IdempierePasswordHasher implements LegacyPasswordHasherInterface
  7. {
  8. use CheckPasswordLengthTrait;
  9. public function hash(string $plainPassword, string $salt = null): string
  10. {
  11. if ($this->isPasswordTooLong($plainPassword)) {
  12. throw new InvalidPasswordException();
  13. }
  14. $hashedPassword = self::Encode($plainPassword, $salt);
  15. return $hashedPassword;
  16. }
  17. public function verify(string $hashedPassword, string $plainPassword, string $salt = null): bool
  18. {
  19. if ('' === $plainPassword || $this->isPasswordTooLong($plainPassword)) {
  20. return false;
  21. }
  22. $passwordIsValid = ( $hashedPassword === $this->hash($plainPassword, $salt) ) ;
  23. return $passwordIsValid;
  24. }
  25. public function needsRehash(string $hashedPassword): bool
  26. {
  27. return true;
  28. }
  29. /**
  30. * Codifica la clave con el mismo proceso que iDempiere
  31. * @param string $salt Cadena de codificacion
  32. * @param string $clave Clave secreta de usuario
  33. * @return string $seed Clave convertia
  34. */
  35. public static function Encode(String $password, String $salt){
  36. $seed = hash("sha512", self::Hex2String($salt) . $password, true);
  37. for($i = 0; $i < 1000; $i++){
  38. $seed = hash("sha512", $seed, true);
  39. }
  40. return self::String2Hex($seed);
  41. }
  42. /**
  43. * Convertir datos binarios en valores hexadecimales
  44. * @param string $string
  45. * @return string
  46. */
  47. public static function String2Hex($string){ return bin2hex($string); }
  48. /**
  49. * Convertir una valores hexadecimales en datos binarios
  50. * @param string $string
  51. * @return string
  52. */
  53. public static function Hex2String($hex){ return hex2bin($hex); }
  54. }