Skip to content

Instantly share code, notes, and snippets.

@Ferrmolina
Last active October 17, 2016 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ferrmolina/b90507d9532459c7ad6050a6688c6c8a to your computer and use it in GitHub Desktop.
Save Ferrmolina/b90507d9532459c7ad6050a6688c6c8a to your computer and use it in GitHub Desktop.
Comparar passwords
<?php
/**
* En base al usuario, y la contraseña que el usuario envía, buscar el nombre de usuario en la base de datos,
* devolver el hash almacenado y guardarlo en una variable. Con el hash almacenado, usar crypt() para generar hash.
* Si ambos hash (con hash_equals) son iguales, devuelve true, si no, false.
* @param string: $initialPassword | Password provided by user
* @param string: $username | Username provided by user
* @return string
*/
private function comparePassword($initialPassword, $username) {
$mysqli = $this->db->conexion();
$sql = $mysqli->prepare("SELECT username, password FROM usuarios WHERE username = ?");
$sql->bind_param("s", $username);
$sql->bind_result($username, $hashInDatabase);
$sql->execute();
while ($sql->fetch()) {
$hashAComprobar = crypt($initialPassword, $hashInDatabase);
$passwordHashingResult = hash_equals($hashInDatabase, $hashAComprobar);
$mysqli->close();
return ($passwordHashingResult) ? self::MESSAGE_LOGIN_SUCCESS : self::MESSAGE_LOGIN_ERROR;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment