Following this tutorial: http://www.gregboggs.com/php-blowfish-random-salted-passwords/ i've been trying to implement encryption for passwords in my registration form. At the moment the code I have runs without any errors but no data is being added to the database. i know the SQL statement is correct as it was working before I started implementing the encryption features.
Here is what I have so far:
<?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 VERSION NOTE: As WAMP server only currently supports php5.4.12 natively, I am using this compatibility library: https://github.com/ircmaxell/password_compat.
BUMP: I've been going over this for a while now and I still can't find why the data isn't being inserted. I tested the SQL statement again. I echoed $password1, $bcrypt_salt, $hashed_password through-out the code to make sure they were working right and those variables all contain the correct info. Any ideas?