我正在使用加密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,表示现有用户的密码匹配。但是,这两个哈希值似乎根本不匹配。请问我漏了什么?我该如何实现这一点?我想确保用户在更改新密码之前与其现有密码匹配。非常感谢任何帮助。
===
来比较哈希值。应该使用crypto.timingSafeEqual
。 - Tofandel