<?php
namespace App\Security\Hasher;
use Symfony\Component\PasswordHasher\Exception\InvalidPasswordException;
use Symfony\Component\PasswordHasher\Hasher\CheckPasswordLengthTrait;
use Symfony\Component\PasswordHasher\LegacyPasswordHasherInterface;
class IdempierePasswordHasher implements LegacyPasswordHasherInterface
{
use CheckPasswordLengthTrait;
public function hash(string $plainPassword, string $salt = null): string
{
if ($this->isPasswordTooLong($plainPassword)) {
throw new InvalidPasswordException();
}
$hashedPassword = self::Encode($plainPassword, $salt);
return $hashedPassword;
}
public function verify(string $hashedPassword, string $plainPassword, string $salt = null): bool
{
if ('' === $plainPassword || $this->isPasswordTooLong($plainPassword)) {
return false;
}
$passwordIsValid = ( $hashedPassword === $this->hash($plainPassword, $salt) ) ;
return $passwordIsValid;
}
public function needsRehash(string $hashedPassword): bool
{
return true;
}
/**
* Codifica la clave con el mismo proceso que iDempiere
* @param string $salt Cadena de codificacion
* @param string $clave Clave secreta de usuario
* @return string $seed Clave convertia
*/
public static function Encode(String $password, String $salt){
$seed = hash("sha512", self::Hex2String($salt) . $password, true);
for($i = 0; $i < 1000; $i++){
$seed = hash("sha512", $seed, true);
}
return self::String2Hex($seed);
}
/**
* Convertir datos binarios en valores hexadecimales
* @param string $string
* @return string
*/
public static function String2Hex($string){ return bin2hex($string); }
/**
* Convertir una valores hexadecimales en datos binarios
* @param string $string
* @return string
*/
public static function Hex2String($hex){ return hex2bin($hex); }
}