结合哈希密码Node.JS时出现PBKDF2错误
binding.PBKDF2 error when hashing passwords Node.JS
我正试图用crypto.PBKDF2
在用户模型中散列我的密码,但我的validatePassword方法失败,出现以下异常
返回绑定。PBKDF2(密码、salt、迭代、keylen、摘要、回调);
这是全错误
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;
};
您的代码有些混乱,它将一个变量设置为异步函数,并试图在函数中使用它?
crypto
方法有一个回调,其中生成的密钥是第二个参数,这就是通常使用它们的方式
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 = function(password){
var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
return this.hash === hash;
};
请注意,您的setPassword
和validatePassword
方法只适用于一个实例,这可能适合测试,但如果您想要多个用户,则可能需要一个数据库来保存这些值,而不仅仅是将它们分配给this
。
您得到的错误是因为您试图将异步函数返回的结果传递给new Buffer
,而不是生成的密钥
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 试图在引导模式内动态生成图表,得到offsetWidth错误
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- 我如何修复包含在captcha的addthis中的错误
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 节点是否需要模块传递带有方括号的arg?这是个错误吗
- Webpack/Rect:遵循egghead.io教程,但出现错误:您可能需要一个合适的加载程序来处理此文件类型
- CKFinder 3为所选文件返回错误的URL
- 同位素库错误:未捕获错误无布局模式包装生产线8
- 铬:“;未捕获的语法错误:意外的标记:"
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- 相位器状态未捕获参考错误
- /undefined在我的404错误日志中多次出现
- Javascript未捕获语法错误意外的标识符错误
- javascript:如何在antlr生成的Lexer中进行错误处理
- Angularjs工厂注入错误
- 结合哈希密码Node.JS时出现PBKDF2错误
- 我得到了错误“返回绑定”.PBKDF2(密码、盐、迭代、keylen、回调);^ TypeError: Not a bu