【问题标题】:How to verify passwords with nodejs?如何使用nodejs验证密码?
【发布时间】:2020-12-22 18:20:40
【问题描述】:

我正在将我的 python 烧瓶应用程序移动到 nodejs。以前我用werkzeug.security.generate_password_hash(password) 对密码进行哈希处理。如何验证nodejs中的密码哈希?代码如下:

async function verify(password, hash) {
    return new Promise((resolve, reject) => {
        const [salt, key] = hash.split(":")
        crypto.scrypt(password, salt, hash.length, (err, derivedKey) => {
            if (err) reject(err);
            resolve(key == derivedKey.toString('hex'))
        });
    })
}

每当我尝试代码时,它都会返回false。有人可以向我解释解决方案吗?

【问题讨论】:

标签: python node.js flask


【解决方案1】:

您应该首先获取用户数据以获取哈希密码和哈希密钥,然后使用保存的密钥加密传递的密码,然后检查它是否等于哈希值。

  // passing the incoming password from the user and the user data 
  // user = {password: '<the hased passowrd>', hashKey: '<the salt hash key for this user>'}
  async function verify(password, user) {
    const requestedPassword = await this.encryptPassword(password, user.hashKey);
    return requestedPassword === user.password;
  }

  async function encryptPassword(password, hashKey) {
    return await bcrypt.hash(password, hashKey);
  }

【讨论】:

  • 这个方法与werkzeug兼容吗?
猜你喜欢
  • 1970-01-01
  • 2020-02-23
  • 2021-08-29
  • 2020-04-06
  • 2019-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多