Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node's bcrypt Compare Returns False Though It's True

Tags:

node.js

bcrypt

This is driving me crazy. Here is the very simple code:

user = new User(_.pick(userData, ['name', 'email', 'password']));
const salt = await bcrypt.genSalt(15);
user.password = await bcrypt.hash(user.password, salt);
const samePass = await bcrypt.compare(user.password, userData.password);

samePass is always false. I have checked sample request password and the hash on https://bcrypt-generator.com and it does show them matched. bcrypt is always returning false. And you can see that the database has nothing to do with it.

Sample data (I have got both using the IDE debugger with a breakpoint at the samePass line):

user.password = $2b$10$nH2SGNZu9rdyz.V6qpT29eAhxKWfiOIr9ojOi96Ye2lQub.Pglof.
userData.password = oXmZ9pG5T4XtndH%#@A
like image 775
Abdalrahman Shatou Avatar asked Jun 22 '26 09:06

Abdalrahman Shatou


2 Answers

user.password = await bcrypt.hash(user.password, salt); //This line is generating the password hash

While comparing you pass the second argument as password hash stored in the database. But, here in your code, you are overriding the input user.password which contains the actual password which needs to be compared

const samePass = await bcrypt.compare(user.password, userData.password);

Change it to:

let passwordHash = await bcrypt.hash(user.password, salt);
const booleanResult = await bcrypt.compare(userData.password, passwordHash);
like image 197
kavigun Avatar answered Jun 25 '26 01:06

kavigun


bcrypt.compare takes two parameters: plaintextPassword and hashedPassword, in that order.

$2b$10$nH2SGNZu9rdyz.V6qpT29eAhxKWfiOIr9ojOi96Ye2lQub.Pglof. is a hashed version of oXmZ9pG5T4XtndH%#@A, so you can do this:

const password = 'oXmZ9pG5T4XtndH%#@A'
const preHashedPassword = '$2b$10$nH2SGNZu9rdyz.V6qpT29eAhxKWfiOIr9ojOi96Ye2lQub.Pglof.'

await bcrypt.compare(password, preHashedPassword) // true

You can also do this:

const salt = await bcrypt.genSalt(15)
const newHashedPassword = await bcrypt.hash(password, salt)

await bcrypt.compare(password, newHashedPassword) // true

But you can't do any of these:

await bcrypt.compare(preHashedPassword, newHashedPassword) // false
await bcrypt.compare(newHashedPassword, preHashedPassword) // false
await bcrypt.compare(preHashedPassword, password) // false
await bcrypt.compare(newHashedPassword, password) // false

RunKit demo

like image 26
Lionel Rowe Avatar answered Jun 25 '26 02:06

Lionel Rowe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!