Node.bcrypt.js: Any way to get same encrypted value when same data and salt are provided?

Created on 22 Mar 2018  路  7Comments  路  Source: kelektiv/node.bcrypt.js

Is there any way that bcrypt.hashSync(value, salt) could return the same value when run twice with the same arguments?

We have a requirement to be able to compare passwords directly in the database, which requires us to calculate the hash value in the application and pass it to the database to do a direct string comparison against the already encrypted password stored there. However this doesn't work, since different values are returned for the same input.

Based on my understanding of how bcrypt works, I think it's not the right tool for our requirements, have I got it right?

question

Most helpful comment

I'd be looking to pass a pre-determined salt rather than a generated one so that I get the same output value though.

All 7 comments

bcrypt is deterministic. If you pass same salt and value, you must get the same value in return.

We have a requirement to be able to compare passwords directly in the database, which requires us to calculate the hash value in the application and pass it to the database to do a direct string comparison against the already encrypted password stored there. However this doesn't work, since different values are returned for the same input.

While I won't say if it is a good approach or not. There are few other things you need to take care of. bcrypt hashes are generated using the crypt hash format: $<version>$<rounds>$<salt><hash> So, it's different than other hashes (message digests).

You need to parse the rounds and salt from the returned hash and keep them separately.

Also note that hashSync accepts a crypt hash header: $<version>$rounds$<salt>

So to get the deterministic output I would call bcrypt.hashSync with the value and the salt, and the salt would have the format of $<version>$rounds$<salt>? Simply just appending the salt to the end of that version and round string? Or I need to generate the salt somehow?

We have a secure salt generator: genSalt(rounds: Number)
See: https://github.com/kelektiv/node.bcrypt.js#api

I'd be looking to pass a pre-determined salt rather than a generated one so that I get the same output value though.

// Load hash from your password DB.
bcrypt.compare(myPlaintextPassword, hash, function(err, res) {
// res == true
});
bcrypt.compare(someOtherPlaintextPassword, hash, function(err, res) {
// res == false
});

Here is compare bcrypt hashed password and plaintext password , this is weird function because we need hash password in UI side before submit it to server for security reason and we need read hash from database in server side , but this comparing to enforce use only can encode the password instead hash password in UI side

this is weird function because we need hash password in UI side before submit it to server for security reason.

You can use a simple ===. But it is dangerous.

I will advice against this. This actually weakens the protection provided by using a hash function - such as, to avoid leaking credentials when the server database is (partially) leaked. An adversary, if got hold of the encrypted passwords somehow and simply pass them to get authenticated.

Also, string comparison based security will open up your apps to timing attacks.

Database is leaked partially to expose the hashed password and not get into application yet, hashed password is still able to prevent hacker from attacking application, comparing hashed passwords being leaked partially from database, the plaintext password or base 64 encoded password goes through from login page before submit page, the X-agent or Trojan virus reveal them or cracked SSL network importer certificate or be intercepted, which is more dangerous than database partially leaking. Actually I create Front End Encryption mechanism to encrypt password much early before submitting login page when we need plaintext password, via decrypt it in server side, to be compared to brypt's hash by brypt comparing method, we still can save crypt hash in database.

Was this page helpful?
0 / 5 - 0 ratings