Files
2026-01-06 15:35:59 -05:00

67 lines
1.9 KiB
PHP

<?php
namespace App\Hashing;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Support\Str;
class PBKDF2Hasher implements Hasher
{
protected $iterations;
protected $saltLength;
protected $algorithm;
public function __construct()
{
$this->iterations = 870000; // Debe coincidir con las iteraciones en Django
$this->saltLength = 22; // Puedes ajustar el tamaño del salt según lo que prefieras
$this->algorithm = 'sha256'; // Algoritmo utilizado (puede ser sha256, sha512, etc.)
}
public function make($value, array $options = [])
{
$salt = Str::random($this->saltLength); // Genera un salt aleatorio de 22 caracteres
$hash = hash_pbkdf2($this->algorithm, $value, $salt, $this->iterations, 64, false);
return "pbkdf2_sha256$$this->iterations$$salt$$hash";
}
public function check($value, $hashedValue, array $options = [])
{
$parts = explode('$', $hashedValue);
if (count($parts) !== 4) {
return false;
}
list(, $iterations, $salt, $hash) = $parts;
// Verificar si la contraseña coincide
$newHash = hash_pbkdf2($this->algorithm, $value, $salt, $iterations, 64, false);
return hash_equals($newHash, $hash); // Comparar hashes de manera segura
}
public function info($hashedValue)
{
$parts = explode('$', $hashedValue);
if (count($parts) !== 4) {
return [];
}
list(, $iterations, $salt, $hash) = $parts;
return [
'algorithm' => $this->algorithm,
'iterations' => (int) $iterations,
'salt' => $salt,
'hash' => $hash,
];
}
public function needsRehash($hashedValue, array $options = [])
{
// Si necesitas implementar una lógica para verificar si el hash necesita rehacerse
return false;
}
}