我得到了错误“返回绑定”.PBKDF2(密码、盐、迭代、keylen、回调);^ TypeError: Not a bu

I'm getting the error "return binding.PBKDF2(password, salt, iterations, keylen, callback); ^ TypeError: Not a buffer"

本文关键字:回调 keylen 迭代 bu Not TypeError 密码 错误 返回 绑定 PBKDF2      更新时间:2023-09-26

查看下面的完整错误代码:

<>之前crypto.js: 601返回绑定。PBKDF2(密码、盐、迭代、keylen、回调);^类型错误:不是缓冲区在pbkdf2 (crypto.js:601:20)在Object.exports。pbkdf2Sync (crypto.js 592:10):at model.UserSchema.methods.hashPassword (/Users/markie13/documents/mean/meanPassport/app/models/user.server.model.js:59:16)at model.UserSchema.methods.authenticate (/Users/markie13/documents/mean/meanPassport/app/models/user.server.model.js:63:32)在查询。(/用户/markie13/文档/说/meanPassport/config/策略/local.js: 19:14)。在/用户/markie13/文件/说/meanPassport/node_modules/猫鼬/node_modules/kareem/index.js: 177:19在/用户/markie13/文件/说/meanPassport/node_modules/猫鼬/node_modules/kareem/index.js: 109:16在过程。_tickCallback (node . js: 442:13)之前

我正在研读Amos Haviv的书《MEAN Stack Development》,读到第6章了——如果有人关心的话。

下面是抛出错误的代码(查看行号的注释):
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
};

所以,事实证明,我是用一个非散列字符串填充mongodb数据库的密码。

当我用数据库中的散列密码登录时,它工作得很好。

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