我收到错误“返回binding.PBKDF2(密码,盐,迭代,keylen,callback); ^ TypeError:不是一个缓冲区“

请参阅下面的完整错误代码:

 crypto.js:601
    返回binding.PBKDF2(密码,盐,迭代,keylen,callback);
                    ^
 TypeError:不是缓冲区
    在pbkdf2(crypto.js:601:20)
    在Object.exports.pbkdf2Sync(crypto.js:592:10)
    在model.UserSchema.methods.hashPassword(/Users/markie13/documents/mean/meanPassport/app/models/user.server.model.js:59:16)
    在model.UserSchema.methods.authenticate(/Users/markie13/documents/mean/meanPassport/app/models/user.server.model.js:63:32)
    在查询。  (/Users/markie13/documents/mean/meanPassport/config/strategies/local.js:19:14)
     at /Users/markie13/documents/mean/meanPassport/node_modules/mongoose/node_modules/kareem/index.js:177:19
     at /Users/markie13/documents/mean/meanPassport/node_modules/mongoose/node_modules/kareem/index.js:109:16
    在process._tickCallback(node.js:442:13)

我正在开发阿莫斯·哈维夫(Amos Haviv)的“MEAN Stack Development”一书,并在第六章中介绍 – 如果有人在意。

这是抛出错误的代码(请参阅行号的注释):

UserSchema.pre('save', function (next) { if (this.password) { this.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64'); this.password = this.hashPassword(this.password); } next(); }); UserSchema.methods.hashPassword = function (password) { return crypto.pbkdf2Sync(password, this.salt, 10000, 64).toString('base64');//line 59 }; UserSchema.methods.authenticate = function (password) { return this.password === this.hashPassword(password);//line 63 }; 

所以,事实certificate,我是用一个非散列string填充密码的Mongo数据库。

一旦我用数据库中的哈希密码login,它工作正常。

现在我需要学习错误检查…

Interesting Posts