【问题标题】:binding.PBKDF2 error when hashing passwords Node.JS散列密码Node.JS时的binding.PBKDF2错误
【发布时间】:2016-04-14 12:57:57
【问题描述】:

我正在尝试使用 crypto.PBKDF2 在用户模型中对我的密码进行哈希处理,但我的 validatePassword 方法失​​败并出现以下异常

return binding.PBKDF2(密码、盐、迭代、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)

这些是相关的方法

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 = new function(password){
    var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
    return this.hash = hash;
};

这里是完整代码的链接:Repo

【问题讨论】:

    标签: javascript node.js authentication encryption pbkdf2


    【解决方案1】:

    您的代码有些混乱,它将一个变量设置为一个异步函数,并以某种方式尝试在函数内部使用它?

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

    请注意,您的 setPasswordvalidatePassword 方法仅适用于一个实例,这可能适合测试,但如果您想要多个用户,您可能需要一个数据库来保存这些值,而不仅仅是分配他们给this

    您遇到的错误是因为您试图将异步函数的返回结果传递给new Buffer,而不是生成的密钥

    【讨论】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2012-12-10
      • 2021-03-16
      • 2014-06-09
      • 2014-05-14
      • 2019-07-20
      • 2020-08-09
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多