Last active
October 13, 2016 09:10
-
-
Save mpratt/5671743 to your computer and use it in GitHub Desktop.
Más info en: http://www.michael-pratt.com/blog/8/Seguridad-en-el-almacenamiento-de-PasswordsContrasenas/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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!'); } | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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