sql >> Base de Datos >  >> RDS >> Mysql

Salando mis hashes con PHP y MySQL

En primer lugar, su DBMS (MySQL) no necesita soporte para hashes criptográficos. Puedes hacer todo eso en el lado de PHP, y eso también es lo que debes hacer.

Si desea almacenar sal y hash en la misma columna, debe concatenarlos.

// the plaintext password
$password = (string) $_GET['password'];

// you'll want better RNG in reality
// make sure number is 4 chars long
$salt = str_pad((string) rand(1, 1000), 4, '0', STR_PAD_LEFT);

// you may want to use more measures here too
// concatenate hash with salt
$user_password = sha512($password . $salt) . $salt;

Ahora, si quieres verificar una contraseña, haz lo siguiente:

// the plaintext password
$password = (string) $_GET['password'];

// the hash from the db
$user_password = $row['user_password'];

// extract the salt
// just cut off the last 4 chars
$salt = substr($user_password, -4);
$hash = substr($user_password, 0, -4);

// verify
if (sha512($password . $salt) == $hash) {
  echo 'match';
}

Es posible que desee echar un vistazo a phpass , que también utiliza esta técnica. Es una solución hash de PHP que utiliza salado entre otras cosas.

Definitivamente deberías echarle un vistazo a la respuesta a la pregunta a la que se vinculó WolfOdrade.