繁体   English   中英

nodejs pbkdf2sync TypeError:密码短语必须是缓冲区

[英]nodejs pbkdf2sync TypeError: Pass phrase must be a buffer

我正在研究平均堆栈应用程序。 我正在使用加密进行密码加密,但是它抛出以下错误。

TypeError:密码短语必须是缓冲区

TypeError:密码短语必须是model.userSchema.methods.setPassword(C:\\ CMT_Platform \\ server \\)的Object.exports.pbkdf2Sync(crypto.js:687:10)的pbkdf2(crypto.js:702:20)的缓冲区出口对象(C:\\ CMT_Platform \\)处的Object.module.exports.register(C:\\ CMT_Platform \\ server \\ services \\ auth.service.js:16:13)处的models \\ user.model.js:24:24)下一步在Layer.handle的server \\ controllers \\ auth.controller.js:22:39 [作为handle_request](C:\\ CMT_Platform \\ node_modules \\ express \\ lib \\ router \\ layer.js:95:5)在下一个(C:\\在Layer.handle处的Route.dispatch(C:\\ CMT_Platform \\ node_modules \\ express \\ lib \\ router \\ route.js:112:3)处的CMT_Platform \\ node_modules \\ express \\ lib \\ router \\ route.js:137:13 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)在Function.handle(C:\\ CMT_Platform \\ node_modules \\ express \\ lib \\ router \\ index.js:275:10)( C:\\ CMT_P 位于Layer.handle处的路由器(C:\\ CMT_Platform \\ node_modules \\ express \\ lib \\ router \\ index.js:47:12)处的latform \\ node_modules \\ express \\ lib \\ router \\ index.js:174:3)[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 /注册HTTP / 1.1” 400 42“-”“ Mozilla / 5.0(Windows NT 10.0; Win64平台; x64)AppleWebKit / 537.36(KHTML,例如Gecko)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;
};

有人是否可以面对这个问题,可以请我指导或启发我。 让我知道是否需要更多详细信息。

谢谢

crypto.pbkdf2Sync返回Buffer并且您将其存储在字符串中,或​​者可以将buffer转换为字符串并进行存储。

您可以将hast类型更改为Buffer,然后尝试在此处检查Mongoose数据类型

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM