【问题标题】:why is it all the brcypt or crypto hashes comparison are always returning false even for correct password为什么即使密码正确,所有 brcypt 或加密哈希比较总是返回 false
【发布时间】:2019-05-02 12:34:39
【问题描述】:

我已经设法使用 cryptojs 和 bcrypt 来散列/加密我所有的密码,但失败了 比较哈希(数据库中的哈希密码与哈希输入密码)总是返回 false 所以我做了更多的挖掘来找出哈希的内容,这些就是结果。

const crypto = require('crypto')
function setUserPassword(inputPassword){
    const salt = crypto.randomBytes(16).toString('hex')
  let hashedPassword = crypto.pbkdf2Sync(inputPassword, salt, 1000, 16,'sha512').toString('hex')
   return{ //we shall store them in the database later
     salt: salt,
     hashedPassword: hashedPassword
   }
}
database ====>ac0f74b30c94fedbbd591889c4705607  //works perefectly using the above function

challenge comes when validating the user password.. using this function..
   function validateUserPassword(enteredPassword, dbSalt, dbPassword){

// then checks if this generated hash is equal to user's hash in the database or not 
   let hashInput = crypto.pbkdf2Sync(enteredPassword, dbSalt, 1000,16, 'sha512') //the same as above

   //u must compare the hashed password in the db with hashedInput password
   return hashInput === dbPassword //IF it returns true then they match
}
so i checked the  hashInput and discovered that it was a buffer instead of the string... 
hey hashed input password  <Buffer ac 0f 74 b3 0c 94 fe db bd 59 18 89 c4 70 56 07>

//may nodejs version... v6.11.4 and alo tried using v10.15.0 but all in  the vain.

【问题讨论】:

  • let hashInput = crypto.pbkdf2Sync(enteredPassword, dbSalt, 1000,16, 'sha512') //the same as above.... ..toString('hex') 不同
  • 非常感谢,我去看看

标签: node.js bcrypt cryptojs


【解决方案1】:

setUserPassword() 中,您正在从哈希函数返回的缓冲区创建一个十六进制编码的字符串,但您忘记在validateUserPassword() 中执行相同的操作。这将解决它:

let hashInput = crypto.pbkdf2Sync(enteredPassword, dbSalt, 1000,16, 'sha512').toString('hex')

【讨论】:

  • 谢谢,终于成功了,现在返回 true ,让我也试试看 bcrypt 例子
猜你喜欢
  • 2020-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-26
  • 2023-03-29
  • 2017-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多