Akki619
Akki619

Reputation: 2432

nodejs pbkdf2sync TypeError: Pass phrase must be a buffer

I am working on mean stack application. I am using crypto for password encryption but it is throwing below error.

TypeError: Pass phrase must be a buffer

TypeError: Pass phrase must be a buffer at pbkdf2 (crypto.js:702:20) at Object.exports.pbkdf2Sync (crypto.js:687:10) at model.userSchema.methods.setPassword (C:\CMT_Platform\server\models\user.model.js:24:24) at Object.module.exports.register (C:\CMT_Platform\server\services\auth.service.js:16:13) at exports.register (C:\CMT_Platform\server\controllers\auth.controller.js:22:39) at Layer.handle [as handle_request] (C:\CMT_Platform\node_modules\express\lib\router\layer.js:95:5) at next (C:\CMT_Platform\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\CMT_Platform\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\CMT_Platform\node_modules\express\lib\router\layer.js:95:5) at C:\CMT_Platform\node_modules\express\lib\router\index.js:281:22 at Function.process_params (C:\CMT_Platform\node_modules\express\lib\router\index.js:335:12) at next (C:\CMT_Platform\node_modules\express\lib\router\index.js:275:10) at Function.handle (C:\CMT_Platform\node_modules\express\lib\router\index.js:174:3) at router (C:\CMT_Platform\node_modules\express\lib\router\index.js:47:12) at Layer.handle [as handle_request] (C:\CMT_Platform\node_modules\express\lib\router\layer.js:95:5) at 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, like Gecko) Chrome/68.0.3440.106 Safari/537.36"

Code:

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;
};

Can somebody please guide me or enlighten me whether they have face this issue or not. Let me know if you want any more details.

Thanks

Upvotes: 0

Views: 974

Answers (1)

Rahul Sharma
Rahul Sharma

Reputation: 10111

crypto.pbkdf2Sync returns Buffer and you are storing in the string OR you can convert buffer to string and store.

You can change the hast type to the Buffer and try Check mongoose data types here

Upvotes: 0

Related Questions