Node.js hashing of passwords

前端 未结 6 563
小鲜肉
小鲜肉 2021-01-29 23:25

I am currently using the following for hashing passwords:

var pass_shasum = crypto.createHash(\'sha256\').update(req.body.password).digest(\'hex\');
6条回答
  •  生来不讨喜
    2021-01-30 00:19

    Try using Bcrypt, it secures the password using hashing.

    bcrypt.hash(req.body.password, salt, (err, encrypted) => {
        user.password = encrypted
        next()
    })
    

    Where salt is the cost value which specifies the strength of hashing. While logging in, compare the password using bcrypt.compare method:

     bcrypt.compare(password, user.password, (err, same) => {
          if (same) {
               req.session.userId = user._id
               res.redirect('/bloglist')
          } else {
               res.end('pass wrong')
          }
     })
    

    For more info, refer to this blog: https://medium.com/@nitinmanocha16/bcrypt-and-nodejs-e00a0d1df91f

提交回复
热议问题