Javascript 哈希密码Node.JS时binding.PBKDF2错误

Javascript 哈希密码Node.JS时binding.PBKDF2错误,javascript,node.js,authentication,encryption,pbkdf2,Javascript,Node.js,Authentication,Encryption,Pbkdf2,我试图在用户模型中使用crypto.PBKDF2散列我的密码,但我的validatePassword方法失败,出现以下异常 返回binding.PBKDF2(密码、salt、迭代、keylen、摘要、回调) 这是全部错误 crypto.js:562 return binding.PBKDF2(password, salt, iterations, keylen, digest, callback); ^ TypeError: Not a buffer at

我试图在用户模型中使用
crypto.PBKDF2
散列我的密码,但我的validatePassword方法失败,出现以下异常

返回binding.PBKDF2(密码、salt、迭代、keylen、摘要、回调)

这是全部错误

crypto.js:562
return binding.PBKDF2(password, salt, iterations, keylen, digest, callback);
               ^
TypeError: Not a buffer
    at TypeError (native)
    at pbkdf2 (crypto.js:562:20)
    at Object.exports.pbkdf2Sync (crypto.js:553:10)
    at new <anonymous> (c:\Users\Joseph\news-trends\models\Users.js:25:23)
    at Object.<anonymous> (c:\Users\Joseph\news-trends\models\Users.js:24:39)
    at Module._compile (module.js:398:26)
    at Object.Module._extensions..js (module.js:405:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (c:\Users\Joseph\news-trends\app.js:17:1)
    at Module._compile (module.js:398:26)
    at Object.Module._extensions..js (module.js:405:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)

下面是完整代码的链接:

您的代码有些混乱,它将变量设置为异步函数,并试图在函数中使用它

crypto
方法有一个回调,其中生成的密钥是第二个参数,这是您通常使用它们的方式

UserSchema.methods.setPassword = function(password){

    var self = this;

    crypto.randomBytes(16, function(err, salt){
        if(err){ throw err; }
        self.salt = salt.toString('hex');
    });

    crypto.pbkdf2(password, this.salt, 1000, 64, function(err, hash){
        if(err){ throw err;}
        self.hash = hash.toString('hex');
    });
}

UserSchema.methods.validatePassword = function(password){
    var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
    return this.hash === hash;
};
请注意,您的
setPassword
validatePassword
方法仅适用于一个实例,这可能适合于测试,但如果您需要多个用户,则可能需要一个数据库来保存这些值,而不仅仅是将它们分配给


您遇到的错误是因为您试图将异步函数返回的结果传递给
新缓冲区
,而不是生成的键您的代码有点混乱,它将变量设置为异步函数,并试图在函数内部使用它

crypto
方法有一个回调,其中生成的密钥是第二个参数,这是您通常使用它们的方式

UserSchema.methods.setPassword = function(password){

    var self = this;

    crypto.randomBytes(16, function(err, salt){
        if(err){ throw err; }
        self.salt = salt.toString('hex');
    });

    crypto.pbkdf2(password, this.salt, 1000, 64, function(err, hash){
        if(err){ throw err;}
        self.hash = hash.toString('hex');
    });
}

UserSchema.methods.validatePassword = function(password){
    var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
    return this.hash === hash;
};
请注意,您的
setPassword
validatePassword
方法仅适用于一个实例,这可能适合于测试,但如果您需要多个用户,则可能需要一个数据库来保存这些值,而不仅仅是将它们分配给


您遇到的错误是因为您试图将异步函数返回的结果传递到新缓冲区,而不是生成的密钥。我知道时间很晚了,但是如果有人仍然面临这个问题,这就是我所做的,它解决了我的问题

UserSchema.methods.setPassword = function(password){
    this.salt = crypto.randomBytes(16).toString('hex');
    this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
}

UserSchema.methods.validatePassword = function(password){
    var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
    return this.hash === hash;
};

我知道已经很晚了,但如果有人仍然面临这个问题,这就是我所做的,它解决了我的问题

UserSchema.methods.setPassword = function(password){
    this.salt = crypto.randomBytes(16).toString('hex');
    this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
}

UserSchema.methods.validatePassword = function(password){
    var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
    return this.hash === hash;
};

我添加了您的代码并相应地修改了问题。我还更改了pbkdf2Sync方法,但错误仍然存在@adeneo@MultiplisJoseph-尝试至少删除上一部分中该函数前面的
new
关键字。我添加了您的代码并相应地更改了问题。我还更改了pbkdf2Sync方法,但错误仍然存在@adeneo@MultiplisJoseph-尝试至少删除最后一部分中该函数前面的
new
关键字。