Question

I am in the process of upgrading the security level of my site.

When researching for the best method to store passwords i found the BCRYPT option in PHP 5.3. I have implemented this function to use a static SALT, however I read that each password should have a different SALT or defeats the purpose.

Should I store the SALT in the database with the user record in plain text? Does this defeat the purpose as well? or should i hash the salt using md5 and store it in the database?

What is the best method when implementing this and storing SALTs?

Was it helpful?

Solution

The modular crypt format for bcrypt does already contain a 128 bit salt. So unless you use an implementation that is different from that, you don’t need another salt.

Note that the main purpose of a salt is to make each input unique so that lookup table assisted dictionary attack against the stored values are impractical.


Jacco reminded me that PHP’s native bcrypt implementation in crypt does not generate a salt automatically. So you have to do it on your own, for example:

$salt = substr(str_replace('+', '.', base64_encode(call_user_func_array('pack', array_merge(array('H14N'), explode('.', uniqid('', true)))).pack('N2', mt_rand(), mt_rand()))), 0, 22);
$hash = crypt($password, '$2a$10$'.$salt.'$');

OTHER TIPS

You should create a new random salt for each user, and store it together with the hash in the database. Most bcrypt implementations already do that. If you look at the output, you'll see that it's a $ separated value containing the salt.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top