I though I understood hashing and salting passwords but it seems I have some misconceptions. I am creating a user account system for my website in nodejs.
The way I understood it was that when a user creates a password we generate a random salt, append it to the password and then hash that string. We can also add a work factor to make the hash work slowly and defend against brute force attacks. We store the salt along with the hash in our database and to validate a login attempt we repeat the above process (on the server) with the stored salt and the attempted password and check to see if the hashes match.
It seems that the bcrypt
module in nodejs is not consistent with my interpretation of hashing. This is from an example at http://codetheory.in/using-the-node-js-bcrypt-module-to-hash-and-safely-store-passwords/
var salt = bcrypt.genSaltSync(10);
var hash = bcrypt.hashSync("my password", salt);
First off why is the work factor applied to the salt rather than the hash? If someone is attacking by brute force they would run the hash function correct? Isn't the hash the function we need to be slow?
I'm also confused by validation with bcrypt:
bcrypt.compareSync("my password", hash);
We need the hashes to be unique even if two users select the same password, this is the point of salt right? So why don't we do this?
bcrypt.compareSync("my password"+salt, hash);
originalPassword = rainbowTable[hash]
. Many salts are even better, but not all systems use more than one salt.