Closed amsaid1989 closed 3 years ago
The 2 bcrypt.compare
callbacks are likely completing in a different, nondeterministic order each time. Try this instead:
bcrypt.compare(password1, hash, function (err, result) {
console.log(result);
bcrypt.compare(password2, hash, function (err, result) {
console.log(result);
});
});
Thanks @scttnlsn. That was it.. not sure why that didn't come to my mind, when it is that simple. In hindsight, it makes a lot of sense.
Thank you very much.
I am testing bcrypt because I want to use for one of my projects, but I am getting inconsistent results from bcrypt.compare function. When I compare the hash with the correct password, sometimes the result of the comparison would be false. This seems to happen if I am comparing multiple passwords, one after the other, to check which of them matches the hash.
Here is the code I am using (stripped from the error handling code for simplification):
And here is the output from a couple of runs of the code:
As you can see, the comparison with
password1
sometimes returnsfalse
.If I remove the comparison with
password2
, things seem to work properly.Code
Output
I checked previous issues that reported the same behavior, but none of them helped me figure out what is causing the issue.
What am I missing here?
Here is my system information: Fedora 34 Workstation bcrypt 5.0.1 node 14.17.0
All the dependencies mentioned for bcrypt on Fedora are installed.