【发布时间】:2016-09-13 17:20:57
【问题描述】:
我正在使用密码https://nodejs.org/api/crypto.html 进行密码加密和身份验证。我正在更改密码页面上,并且无法确定用户提供的密码是否与现有密码具有相同的哈希值。下面是我的代码。
var createSalt = function createSalt() {
return crypto.randomBytes(128).toString('base64');
};
var hashPwd = function hashPwd(salt, pwd) {
var hmac = crypto.createHmac('sha256', salt);
return hmac.update(pwd).digest('hex');
};
//use password , create salt, hash and compare with the existing
var salt = createSalt();
var passHash = hashPwd(salt,data.Password);
console.log('the password is', user.PassHash === passHash);
我期待上面的控制台消息在现有用户密码匹配的地方打印 true。但是,这两个哈希似乎根本不匹配。请问我错过了什么?如何做到这一点?我想确保用户密码与他现有的密码匹配,然后他才能更改新密码。任何帮助,将不胜感激。
【问题讨论】:
标签: node.js hash cryptography