Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to use bcrypt in PHP to safely store passwords (PHP 5.3+ only)
<?php
// secure hashing of passwords using bcrypt, needs PHP 5.3+
// see http://codahale.com/how-to-safely-store-a-password/
// salt for bcrypt needs to be 22 base64 characters (but just [./0-9A-Za-z]), see http://php.net/crypt
$salt = substr(strtr(base64_encode(openssl_random_pseudo_bytes(22)), '+', '.'), 0, 22);
// 2y is the bcrypt algorithm selector, see http://php.net/crypt
// 12 is the workload factor (around 300ms on my Core i7 machine), see http://php.net/crypt
$hash = crypt('foo', '$2y$12$' . $salt);
// we can now use the generated hash as the argument to crypt(), since it too will contain $2y$12$... with a variation of the hash. No need to store the salt anymore, just the hash is enough!
var_dump($hash == crypt('foo', $hash)); // true
var_dump($hash == crypt('bar', $hash)); // false
?>
@grugnog

This comment has been minimized.

Show comment
Hide comment
@grugnog

grugnog May 10, 2012

See http://www.openwall.com/phpass/ for examples of good random sources and base64 generation.

grugnog commented May 10, 2012

See http://www.openwall.com/phpass/ for examples of good random sources and base64 generation.

@Bittarman

This comment has been minimized.

Show comment
Hide comment
@Bittarman

Bittarman May 30, 2013

something more random than base64_encode(sha1(microtime(true), true))), 0, 22) would be:
Instead bin2hex(openssl_random_pseudo_bytes(22));
openssl_random_pseudo_bytes is the most secure prng available in most php distributions

Bittarman commented May 30, 2013

something more random than base64_encode(sha1(microtime(true), true))), 0, 22) would be:
Instead bin2hex(openssl_random_pseudo_bytes(22));
openssl_random_pseudo_bytes is the most secure prng available in most php distributions

@JimWestergren

This comment has been minimized.

Show comment
Hide comment
@JimWestergren

JimWestergren Jul 19, 2013

So this is all you need? :) Great!

$blowfish_salt = bin2hex(openssl_random_pseudo_bytes(22));
$hash = crypt($_POST['password'], "$2a$12$".$blowfish_salt);
// Save the hash but no need to save the salt

if (crypt($_POST['password'], $hash) == $hash) {
    // Verified
}

JimWestergren commented Jul 19, 2013

So this is all you need? :) Great!

$blowfish_salt = bin2hex(openssl_random_pseudo_bytes(22));
$hash = crypt($_POST['password'], "$2a$12$".$blowfish_salt);
// Save the hash but no need to save the salt

if (crypt($_POST['password'], $hash) == $hash) {
    // Verified
}
@nagarjun

This comment has been minimized.

Show comment
Hide comment
@nagarjun

nagarjun Aug 17, 2013

Seems like you do need to save the salt. I can't get the two hashes to match otherwise. Would you recommend having a static salt instead of setting it dynamically and storing it in the database? That way, even if the database was compromised, the hacker would not be able to read the hashes without the salt which is inside your code.

nagarjun commented Aug 17, 2013

Seems like you do need to save the salt. I can't get the two hashes to match otherwise. Would you recommend having a static salt instead of setting it dynamically and storing it in the database? That way, even if the database was compromised, the hacker would not be able to read the hashes without the salt which is inside your code.

@GRAgmLauncher

This comment has been minimized.

Show comment
Hide comment
@GRAgmLauncher

GRAgmLauncher Oct 31, 2013

@ nagarjun, the point of bcrypt and using blowfish ($2a$) is that you set a work factor high enough where it would take someone a very long time to crack a single password even though they have the salt and the final hash right there.

A single static salt that all users would share makes the whole storage mechanism less secure. A random salt per user, with a high enough work factor, is all you need.

GRAgmLauncher commented Oct 31, 2013

@ nagarjun, the point of bcrypt and using blowfish ($2a$) is that you set a work factor high enough where it would take someone a very long time to crack a single password even though they have the salt and the final hash right there.

A single static salt that all users would share makes the whole storage mechanism less secure. A random salt per user, with a high enough work factor, is all you need.

@charleshross

This comment has been minimized.

Show comment
Hide comment
@charleshross

charleshross May 14, 2014

FYI use PHP's built in bcrypt and don't save the salt, but do increase the complexity integer as time goes on, security has never been so syntactically sugary. The function: http://docs.php.net/manual/en/function.password-hash.php

charleshross commented May 14, 2014

FYI use PHP's built in bcrypt and don't save the salt, but do increase the complexity integer as time goes on, security has never been so syntactically sugary. The function: http://docs.php.net/manual/en/function.password-hash.php

@bruce-lim

This comment has been minimized.

Show comment
Hide comment
@bruce-lim

bruce-lim Oct 23, 2014

@charleshross What if you don't have PHP 5.5 installed?

bruce-lim commented Oct 23, 2014

@charleshross What if you don't have PHP 5.5 installed?

@ethanpooley

This comment has been minimized.

Show comment
Hide comment

ethanpooley commented Dec 2, 2014

@bruce-lim Use the password_compat library: https://github.com/ircmaxell/password_compat

@e-ruiz

This comment has been minimized.

Show comment
Hide comment
@e-ruiz

e-ruiz Sep 18, 2015

Please, read this, is very important to understand about secure and insecure salt techniques: https://crackstation.net/hashing-security.htm#phpsourcecode

Here, as you can see, PHP 5.5 has implemented the PBKDF2 noticed by Crackstation:
http://php.net/manual/pt_BR/function.hash-pbkdf2.php.

If you are using PHP 5.5+ use @charleshross tip.

From manuals (http://php.net/password_hash):
"The salt option has been deprecated as of PHP 7.0.0. It is now preferred to simply use the salt that is generated by default."

It because, password_hash() with PASSWORD_DEFAULT uses strong techniques to generate the salt behind the scenes.

Regards.

e-ruiz commented Sep 18, 2015

Please, read this, is very important to understand about secure and insecure salt techniques: https://crackstation.net/hashing-security.htm#phpsourcecode

Here, as you can see, PHP 5.5 has implemented the PBKDF2 noticed by Crackstation:
http://php.net/manual/pt_BR/function.hash-pbkdf2.php.

If you are using PHP 5.5+ use @charleshross tip.

From manuals (http://php.net/password_hash):
"The salt option has been deprecated as of PHP 7.0.0. It is now preferred to simply use the salt that is generated by default."

It because, password_hash() with PASSWORD_DEFAULT uses strong techniques to generate the salt behind the scenes.

Regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment