Skip to content

Instantly share code, notes, and snippets.

@mpratt
Last active October 13, 2016 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mpratt/5671743 to your computer and use it in GitHub Desktop.
Save mpratt/5671743 to your computer and use it in GitHub Desktop.
<?php
$nombre = $_POST['nombre'];
$password = $_POST['pass'];
// Validamos $nombre, bla bla bla..
// Extraemos el hash de la base de datos
$db = new PDO(......);
$stmt = $db->prepare('SELECT pass
FROM usuarios
WHERE nombre = ?');
$stmt->execute(array($nombre));
$dbHash = $stmt->fetchcolumn();
// Recalculamos a ver si el hash coincide.
if (crypt($password, $dbHash) == $dbHash)
echo 'El usuario ha sido autenticado correctamente';
else
die('Mal Password');
?>
<?php
if (defined('CRYPT_BLOWFISH') && CRYPT_BLOWFISH)
{
echo crypt('Mi querido password', '$2y$07$esteesuntextoaleatoreo$');
// Resulta en: $2y$07$esteesuntextoaleatoree24BQCnGmh2nNpnOeQkUe4cw9x191XD6
echo crypt('Mi querido password', '$2y$10$esteesuntextoaleatoreo$');
// Resulta en: $2y$10$esteesuntextoaleatoreekeSBOneABcQ6MqJX3leod5vQkI.RyLS
echo crypt('rasmuslerdorf', '$2y$07$usesomesillystringforsalt$');
// Resulta en: $2y$07$usesomesillystringfore2uDLvp1Ii2e./U9C8sBjqp8I90dH6hi
}
else { die('No hay soporte para Bcrypt!'); }
?>
<?php
/**
* Funcion para hacer más costoso al asunto
*/
function hash_password($password, $salt)
{
$hash = hash_hmac('SHA512', $password, $salt);
for ($i = 0; $i < 5000; $i++)
{
$hash = hash_hmac('SHA512', $hash, $salt);
}
return $hash;
}
$nombre = $_POST['nombre'];
$password = $_POST['password'];
$salt = str_replace('=', '.', base64_encode(mcrypt_create_iv(20)));
$hash = hash_password($password, $salt);
var_dump($hash);
?>
<?php
// Recibo el nombre y password con el que se quiere registrar
$nombre = $_POST['nombre'];
$password = $_POST['password'];
// Genero una sal aleatorea. En este caso uso mcrypt_create_iv y su
// resultado lo traduzco a algo un poco mas "legible"..
$salt = str_replace('=', '.', base64_encode(mcrypt_create_iv(20)));
// Ya teniendo el salt, lo voy a unir al password y genero un hash
// en este caso con SHA512
$hash = hash_hmac('SHA512', $password, $salt);
var_dump($hash);
?>
<?php
$nombre = $_POST['nombre'];
$password = $_POST['pass'];
// Validar que $nombre esté disponible, que si contenga
// un rango de letras, numeros, etc etc.. y luego:
// Generamos un salt aleatoreo, de 22 caracteres para Bcrypt
$salt = substr(base64_encode(openssl_random_pseudo_bytes('30')), 0, 22);
// A Crypt no le gustan los '+' así que los vamos a reemplazar por puntos.
$salt = strtr($salt, array('+' => '.'));
// Generamos el hash
$hash = crypt($password, '$2y$10$' . $salt);
// Guardamos los datos en la base de datos
$db = new PDO(.....);
$stmt = $db->prepare('INSERT INTO usuarios (nombre, pass) VALUES (?, ?)');
$stmt->execute(array($nombre, $hash));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment