node.bcrypt.js icon indicating copy to clipboard operation
node.bcrypt.js copied to clipboard

bcrypt.compare return false always.

Open edgarcheverier opened this issue 7 years ago • 10 comments

node v: v10.13.0 "bcrypt": "^3.0.2"

I console log the passwords and the hash to check if I was doing something wrong, also tried to create the hash with '${req.body.password}' but nothings seems to be working. thanks !

the bcrypt.compare returns false always, when tries to compare hash from the db with the string password.

router.post('/', function (req, res) {
  bcrypt.hash(req.body.password, saltRounds, function(err, hash) {
    client.query(
      `INSERT INTO public."user"(name, last_name, email, password,  phone, photo)
      VALUES($1, $2, $3, $4, $5, $6)`,
    [req.body.name,
      req.body.last_name,
      req.body.email,
      hash,
      req.body.phone,
      req.body.photo])
  })
})

router.post('/login', function(req, res) {
  client.query(
    `SELECT email, password FROM public."user" WHERE email = '${req.body.email}'`,
    function(err, res) {
      if (err) {
        console.log(err)
      } else {
        bcrypt.compare(req.body.password, res.rows[0].password).then(function(res) {
          console.log(res)
        })
      }
    })
})

edgarcheverier avatar Dec 07 '18 17:12 edgarcheverier

Character encoding? Could that be an issue?

recrsn avatar Dec 08 '18 13:12 recrsn

Thank you for the response, the problem seems to be related to the amount of characters, the length must be 60 in order to work.

edgarcheverier avatar Dec 08 '18 15:12 edgarcheverier

i've the same issue with v10.13.0, on v8 works like normal

SaqrWare avatar Dec 13 '18 18:12 SaqrWare

and how can we resolve that? 61 it's not a pattern ?

when we use the bcrypt-generator it generates the sameu number

ricardofilho6 avatar Jan 10 '19 18:01 ricardofilho6

This seems to be a continuing issue are there any updates to specific issue?

joshuaanthonydeleon avatar Jan 11 '19 03:01 joshuaanthonydeleon

In all cases the issue has been with the user input, character encoding, or supplying invalid hashes.

We do not throw an error for a malformed bcrypt hash. This is something what I think we should be revisiting.

recrsn avatar Jan 11 '19 03:01 recrsn

My Character length is 255 in DB, Also before data stored in DB it returns true but at the time of checking again after store (i..e. value from DB) it returns false I checked both strings plain and hash from both sides (i.e. from request and from DB)

nachiket-sazinga avatar Jan 10 '22 06:01 nachiket-sazinga

Solved to me with the hack from https://github.com/kelektiv/node.bcrypt.js/issues/849#issuecomment-882636932 Need to replace $2y$ with $2a$ manually:

const hashPass = /^\$2y\$/.test(user.password) ? '$2a$' + user.password.slice(4) : user.password;

ybelenko avatar Mar 02 '22 00:03 ybelenko

Solved to me with the hack from #849 (comment) Need to replace $2y$ with $2a$ manually:

const hashPass = /^\$2y\$/.test(user.password) ? '$2a$' + user.password.slice(4) : user.password;

Thank you so much @ybelenko a is the hero 😄

faisalqureshi50 avatar Sep 10 '24 16:09 faisalqureshi50

Just incase anyone is still having this issue, PLEASEEEEEEEEE ensure that you aren't double hashing the password from your model section and hashing it again in your routes. That's what the issue was for me.

I was doing this: image

and this: image

image

in my routes and I didn't figure it out until I revisited my model for it again.

Ehiane avatar Oct 29 '24 23:10 Ehiane