遵循本教程:http://www.gregboggs.com/php-blowfish-random-salted-passwords/我一直在尝试在注册表单中实施密码加密。目前我运行的代码没有任何错误,但没有数据添加到数据库。我知道SQL语句是正确的,因为它在我开始实现加密功能之前一直在工作。
这是我到目前为止所做的:
<?php
CRYPT_BLOWFISH or die ('No Blowfish found.');
include_once "config.php";
include_once "lib\password.php";
//This string tells crypt to use blowfish for 5 rounds.
$Blowfish_Pre = '$2a$05$';
$Blowfish_End = '$';
if($_POST["username"] && $_POST["email"] && $_POST["password1"] && $_POST["password2"]) {
if($_POST["password1"] = $_POST["password2"]) {
$password1 = mysql_real_escape_string ($_POST["password1"]);
// Blowfish accepts these characters for salts.
$Allowed_Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./';
$Chars_Len = 63;
// 18 would be secure as well.
$Salt_Length = 21;
$salt = "";
//$mysql_date = date( 'Y-m-d' );
for($i=0; $i<$Salt_Length; $i++)
{
$salt .= $Allowed_Chars[mt_rand(0,$Chars_Len)];
}
$bcrypt_salt = $Blowfish_Pre . $salt . $Blowfish_End;
$hashed_password = crypt($password1, $bcrypt_salt);
/* create a prepared statement */
$stmt = mysqli_prepare($link, "INSERT INTO `users` (`username`, `email`, `password`, `salt`) VALUES (?, ?, ?, ?)");
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "ssss", $_POST["username"], $_POST["email"], $hashed_password, $salt);
/* execute query */
mysqli_stmt_execute($stmt);
/* close statement */
mysqli_stmt_close($stmt);
print "<h1>You have registered sucessfully!</h1>";
print "<a href='main_login.html'>Log in</a>";
}
else print "Your passwords do not match, try again!";
}
else print "Please fill out the entire form!";
/* close connection */
mysqli_close($link);
?>
PHP版本注意: 由于WAMP服务器本身仅支持php5.4.12,因此我使用此兼容性库:https://github.com/ircmaxell/password_compat。
BUMP:我已经讨论了一段时间了,我仍然无法找到未插入数据的原因。我再次测试了SQL语句。我在代码中回显了$ password1,$ bcrypt_salt,$ hashed_password以确保它们正常工作,并且这些变量都包含正确的信息。有什么想法吗?
答案 0 :(得分:1)
如果您只想比较$ password1和$ password2,那么您只需检查:
$hash1 = password_hash("$password1", PASSWORD_BCRYPT, $options)."\n";
$hash2 = password_hash("$password2", PASSWORD_BCRYPT, $options)."\n";
if ($hash1 == $hash2) {
echo 'matched';
}
但这不会非常有用,你想从user和$ hash2获得$ password1 从商店然后:
if (password_verify($password1, $hash2)) {
echo 'matched';
}