Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract the salt out of bcrypt hash passwords in php?

My database stores the bcrypt passwords which means that the salt should be stored with in the password field. I don't want to make a separate field to store the salt by itself when it is not necessary. However when I want to compare passwords that the user sends to me to the passwords stored in the database, I need to hash the incoming password with the same salt. Question: what part of the stored hash is the salt? I think I could just return the salt using simple substr().

// password stored in database.
$user->password_hash = password_hash($password, PASSWORD_BCRYPT, array('cost' => 13));


// password from form being compared to form password

$form_password_hash = password_hash($data['form-password'], PASSWORD_BCRYPT, array('cost' => 13));

if($user->getPasswordHash() == $form_password_hash)
{
    $user->setPassword($data['new-password']);
    return new Response("Your password has been changed");
}
like image 896
Dr.Knowitall Avatar asked Jun 26 '13 22:06

Dr.Knowitall


People also ask

Can bcrypt passwords be decrypted?

You can't decrypt but you can BRUTEFORCE IT... I.E: iterate a password list and check if one of them match with stored hash.

Does bcrypt password salt?

bcrypt uses a 128-bit salt and encrypts a 192-bit magic value. It takes advantage of the fact that the Blowfish algorithm (used in the core of bcrypt for password hashing) needs a fairly expensive key setup, thus considerably slowing down dictionary-based attacks.

Does bcrypt automatically salt?

Another benefit of bcrypt is that it requires a salt by default. Let's take a deeper look at how this hashing function works!

Is it possible to crack bcrypt?

bcrypt is a very hard to crack hashing type, because of the design of this slow hash type that makes it memory hard and GPU-unfriendly (especially with high cost factors).

What is the use of bcrypt in PHP?

The bcrypt is a password hashing technique used to build password security. It is used to protect the password from hacking attacks because of the password is stored in bcrypted format. The password_hash () function in PHP is an inbuilt function which is used to create a new password hash. It uses a strong & robust hashing algorithm.

How to salt and hash a password using bcrypt?

How to salt and hash a password using bcrypt. 1 Step 0: First, install the bcrypt library. $ npm i bcrypt. Now let's look at the code. 2 Step 1: Include the bcrypt module. 3 Step 2: Set a value for saltRounds. 4 Step 3: Declare a password variable. 5 Step 4: Generate a salt. More items

Is it safe to hash passwords in PHP?

The native password hashing API available in PHP 5.5 safely handles both hashing and verifying passwords securely. Using the crypt () function, as it supports several hashing algorithms in PHP 5.3 and later. It is suggested to use the Blowfish algorithm – which is the default option – as opposed to common hashing functions.

Why do we need to salt and hash passwords?

For security purposes, it is essential to salt and hash passwords before storing them in a secure database. Hashing algorithms turn a plain text password into a new fixed-length string called a hash. Before hashing a password, we apply a salt. A salt is a random string that makes the hash unpredictable.


2 Answers

Salt is the first 22 characters after the third $ in the hash:

$2y$13$<this is the salt, 22 chars><this is the password hash>

But you should not manually extract the salt to verify the password - use the password_verify function. It takes the password the user entered as the first argument, and the complete hash as the second argument, and handles the salt correctly.

like image 96
1615903 Avatar answered Sep 28 '22 09:09

1615903


You need to use the password_verify function. This function will parse the hashed password string to find the salt and perform the calculation.

if (password_verify($data['form-password'], $user->getPasswordHash())) {
    echo 'Password is correct';
}
like image 44
joshuahealy Avatar answered Sep 28 '22 08:09

joshuahealy