假设我想为用户存储密码,这是否是使用 PHP 5.5 的password_hash()
功能(或 PHP 5.3.7+ 的此版本:https ://github.com/ircmaxell/password_compat )的正确方法?
$options = array("cost" => 10, "salt" => uniqid());
$hash = password_hash($password, PASSWORD_BCRYPT, $options);
然后我会这样做:
mysql_query("INSERT INTO users(username,password, salt) VALUES($username, $hash, " . $options['salt']);
插入数据库。
然后验证:
$row = mysql_fetch_assoc(mysql_query("SELECT salt FROM users WHERE id=$userid"));
$salt = $row["salt"];
$hash = password_hash($password, PASSWORD_BCRYPT, array("cost" => 10, "salt" => $salt));
if (password_verify($password, $hash) {
// Verified
}