哈希密码时 binding.PBKDF2 错误 Node.JS
binding.PBKDF2 error when hashing passwords Node.JS
我正在尝试使用 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;
};
这里是 link 的完整代码:Repo
您的代码有点混乱,它将变量设置为异步函数,并以某种方式尝试在函数内部使用它?
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
,而不是生成的键
我知道已经晚了,但是如果有人仍然面临这个问题,这就是我所做的,它解决了我的问题。
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.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;
};
这里是 link 的完整代码:Repo
您的代码有点混乱,它将变量设置为异步函数,并以某种方式尝试在函数内部使用它?
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
,而不是生成的键
我知道已经晚了,但是如果有人仍然面临这个问题,这就是我所做的,它解决了我的问题。
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;
};