gpt4 book ai didi

javascript - Node.JS 对密码进行哈希处理时出现 Binding.PBKDF2 错误

转载 作者:行者123 更新时间:2023-11-28 18:49:32 24 4
gpt4 key购买 nike

我尝试使用 crypto.PBKDF2 对用户模型中的密码进行哈希处理,但我的 validatePassword 方法失​​败并出现以下异常

return binding.PBKDF2(password, salt, iterations, keylen, digest, callback);

这是完整的错误

crypto.js:562
return binding.PBKDF2(password, salt, iterations, keylen, digest, callback);
^
TypeError: Not a buffer
at TypeError (native)
at pbkdf2 (crypto.js:562:20)
at Object.exports.pbkdf2Sync (crypto.js:553:10)
at new <anonymous> (c:\Users\Joseph\news-trends\models\Users.js:25:23)
at Object.<anonymous> (c:\Users\Joseph\news-trends\models\Users.js:24:39)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (c:\Users\Joseph\news-trends\app.js:17:1)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)

这些是相关方法

UserSchema.methods.setPassword = function(password){
var self = this;

crypto.randomBytes(16, function(err, salt){
if(err){ throw err; }
self.salt = salt.toString('hex');
});

crypto.pbkdf2(password, this.salt, 1000, 64, function(err, hash){
if(err){ throw err;}
self.hash = hash.toString('hex');
});
};
UserSchema.methods.validatePassword = new function(password){
var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
return this.hash = hash;
};

这里是完整代码的链接:Repo

最佳答案

我知道已经晚了,但是如果有人仍然面临这个问题,这就是我所做的,它解决了我的问题。

UserSchema.methods.setPassword = function(password){
this.salt = crypto.randomBytes(16).toString('hex');
this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
}

UserSchema.methods.validatePassword = function(password){
var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
return this.hash === hash;
};

关于javascript - Node.JS 对密码进行哈希处理时出现 Binding.PBKDF2 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34700837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com