【发布时间】:2019-01-17 23:08:05
【问题描述】:
我正在研究平均堆栈应用程序。我正在使用 crypto 进行密码加密,但它抛出了错误。
TypeError:密码短语必须是缓冲区
TypeError:密码短语必须是缓冲区 在 pbkdf2 (crypto.js:702:20) 在 Object.exports.pbkdf2Sync (crypto.js:687:10) 在 model.userSchema.methods.setPassword (C:\CMT_Platform\server\models\user.model.js:24:24) 在 Object.module.exports.register (C:\CMT_Platform\server\services\auth.service.js:16:13) 在exports.register (C:\CMT_Platform\server\controllers\auth.controller.js:22:39) 在 Layer.handle [as handle_request] (C:\CMT_Platform\node_modules\express\lib\router\layer.js:95:5) 在下一个 (C:\CMT_Platform\node_modules\express\lib\router\route.js:137:13) 在 Route.dispatch (C:\CMT_Platform\node_modules\express\lib\router\route.js:112:3) 在 Layer.handle [as handle_request] (C:\CMT_Platform\node_modules\express\lib\router\layer.js:95:5) 在 C:\CMT_Platform\node_modules\express\lib\router\index.js:281:22 在 Function.process_params (C:\CMT_Platform\node_modules\express\lib\router\index.js:335:12) 在下一个 (C:\CMT_Platform\node_modules\express\lib\router\index.js:275:10) 在 Function.handle (C:\CMT_Platform\node_modules\express\lib\router\index.js:174:3) 在路由器 (C:\CMT_Platform\node_modules\express\lib\router\index.js:47:12) 在 Layer.handle [as handle_request] (C:\CMT_Platform\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix (C:\CMT_Platform\node_modules\express\lib\router\index.js:317:13) ::1 - - [11/Aug/2018:08:33:55 +0000] "POST /register HTTP/1.1" 400 42 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, 像壁虎)Chrome/68.0.3440.106 Safari/537.36"
代码:
var custschema= new mongoose.Schema({
email: { type: String, unique: true, required: true },
name: { type: String, required: true },
role: { type: String },
hash: String,
salt: String
});
custschema.methods.setPassword = function(password) {
this.salt = crypto.randomBytes(16).toString('hex');
//this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64, 'sha512').toString('hex');
var buffer = new Buffer(this.salt, "binary");
console.log(this.salt);
console.log(buffer);
this.hash = crypto.pbkdf2Sync(password, buffer, 1000, 64, 'sha512').toString('hex');
};
custschema.methods.validPassword = function(password) {
//var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64, 'sha512').toString('hex');
var hash = crypto.pbkdf2Sync(password, new Buffer(this.salt,'binary'), 1000, 64, 'sha512').toString('hex');
return this.hash === hash;
};
有人可以指导我或启发我是否面临这个问题。如果您想了解更多详情,请告诉我。
谢谢
【问题讨论】:
-
您的解决方案可以在现有问题中找到,stackoverflow.com/questions/30377119/…
标签: angularjs node.js mean-stack cryptojs