How to use bcrypt for hashing passwords in PHP?

Spread the love

Sometimes, we want to use bcrypt for hashing passwords in PHP.

In this article, we’ll look at how to use bcrypt for hashing passwords in PHP.

How to use bcrypt for hashing passwords in PHP?

To use bcrypt for hashing passwords in PHP., we can use the password_hash function.

For instance, we write

echo password_hash('helloworld', PASSWORD_DEFAULT)."\n";

to call password_hash with the plain password string and PASSWORD_DEFAULT to create a hash from the password with the default settings.

We can also set a few options.

For instance, we write

$options = [
  'cost' => 20
];
echo password_hash('helloworld', PASSWORD_BCRYPT, $options)."\n";

to use PASSWORD_BCRYPT to encrypt the password with bcrypt and 20 rounds.

To verify the password, we use password_verify.

For instance, we write

password_verify('helloworld', $hash)

to check that the $hash password hash has the same hash as 'helloworld'.

Conclusion

To use bcrypt for hashing passwords in PHP., we can use the password_hash function.

Leave a Reply

Your email address will not be published. Required fields are marked *