使用PHP 5.5的password_hash和password_verify函数
假设我想为用户存储密码,这是使用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}