Search code examples
phpcrypt

PHP Crypt encrypt/decrypt not returning the same hash tag


Sign up form - Password is 123456:

$pwd = $_POST['pwd'];
$salt = '$2a$10$R.Baj0mvj5doNvtvzDjwP5$';
$password = crypt("$pwd", $salt);

Hash in the database:

$2a$10$R.Baj0mvj5doNvtvzDjwPuN/W8Z3n6RVGyM0pM

Hash comparison when user is login in - Password again, 123456:

$salt = '$2a$10$R.Baj0mvj5doNvtvzDjwP5$';
$crypt_pass=crypt($password,$salt);

The $crypt_pass = $2a$10$R.Baj0mvj5doNvtvzDjwPuN/W8Z3n6RVGyM0pMQB89k2m9nYRIN6O The password hash in the database is: $2a$10$R.Baj0mvj5doNvtvzDjwPuN/W8Z3n6RVGyM0pM

Why aren't they matching when I'm hashing them with the same salt?


Solution

  • You're using Blowfish as your hash type. That will always return different values for the same string and salt. If you want your hashes to match use SHA 512.

    $salt = '$6$rounds=5000$usesomesillystringforsalt$';