【问题标题】:nodejs pbkdf2sync TypeError: Pass phrase must be a buffernodejs pbkdf2sync TypeError:密码短语必须是缓冲区
【发布时间】: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;
};

有人可以指导我或启发我是否面临这个问题。如果您想了解更多详情,请告诉我。

谢谢

【问题讨论】:

标签: angularjs node.js mean-stack cryptojs


【解决方案1】:

crypto.pbkdf2Sync 返回 Buffer 并且您将存储在字符串中,或​​者您可以将缓冲区转换为字符串并存储。

你可以把hast类型改成Buffer,试试Check mongoose数据类型here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 2017-07-22
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    相关资源
    最近更新 更多