I have login and register system in my website I want to use a powerful way to encrypt users passwords before storing them in my MySQL database . I use this code to do encrypt the passwords :
function better_crypt($input, $rounds = 7)
{
$salt = "";
$salt_chars = array_merge(range('A','Z'), range('a','z'), range(0,9));
for($i=0; $i < 22; $i++) {
$salt .= $salt_chars[array_rand($salt_chars)];
}
return crypt($input, sprintf('$2a$%02d$', $rounds) . $salt);
}
$user_password = "123456";
$password_hash = better_crypt($user_password);
//$password_hash = "$2a$07$t8Omz2TZhz5u0AI3l8uB4uQxzqYZCoqEsQmRo1gr.Viq5UnNReGMy";=> store in database
And when a user try to login I use this to check the password :
$password_entered = "123456";
$database_password_hash = "$2a$07$t8Omz2TZhz5u0AI3l8uB4uQxzqYZCoqEsQmRo1gr.Viq5UnNReGMy";// I get this from database depending on the username
if(crypt($password_entered, $database_password_hash) == $database_password_hash)
{
echo 'password is correct';
}
else
{
echo 'not correct';
}
I use crypt
because my PHP version does not support password_verify
.
My problem is : I still get not correct
all the time .
I want to give each user a different salt' and I want to check it by
crypt`
Do I have to change anything in this code ?why does it give not correct
all the time?