Clase para encriptar y comprobar una contraseña con PHP
<?php
class EncrypterPop {
protected static $slt = 'Contraseñaaaaaa.659';
protected static $method_pass = 'PASSWORD_ARGON2I';
protected static $cost;
function __construct(){
self::$cost = 11;
}
private function checkCostEncrypt(){
$timeTarget = 0.080; // 80 milliseconds
do {
$this->cost++;
$start = microtime(true);
password_hash("test", PASSWORD_DEFAULT, ["cost" => $this->cost]);
$end = microtime(true);
} while (($end - $start) < $timeTarget);
echo $this->cost;
}
public static function encryptPass($pass){
$pasy = hash_hmac("sha512", $pass, self::$slt);
$options = array();
if(self::$method_pass == 'PASSWORD_DEFAULT'):
$options = ['cost' => self::$cost];
elseif(self::$method_pass == 'PASSWORD_ARGON2I'):
// https://stackoverflow.com/questions/47602044/how-do-i-use-the-argon2-algorithm-with-password-hash
$options = ['memory_cost' => 2048, 'time_cost' => 4, 'threads' => 3];
endif;
return password_hash($pasy, PASSWORD_ARGON2I, $options);
}
public static function checkPass($pass,$passDataBase){
$pasy = hash_hmac("sha512", $pass, self::$slt);
if(password_verify($pasy, $passDataBase)):
return true;
else:
return false;
endif;
}
}
?>
$p_post = $_POST['contraseña'];
// Encriptar y la guardamos en la base de datos
$p_encriptada = EncrypterPop::encryptPass($p_post);
// Comprobar contraseña
$p_base_datos = 'Contraseña encriptada de la base de datos';
if(EncrypterPop::checkPass($p_post,$p_base_datos)){
// todo bien
}else{
// la contraseña indicada esta mal
}