简体   繁体   English

nodejs pbkdf2sync不是缓冲区错误

[英]nodejs pbkdf2sync Not a buffer error

Suddenly in my console it's start giving errors, and it has been running fine for days without any change. 突然在我的控制台中开始出现错误,并且连续数天运行良好,没有任何更改。 Please advise. 请指教。

/node_modules/mysql/lib/protocol/Parser.js:82
        throw err;
              ^
TypeError: Not a buffer
    at TypeError (native)
    at pbkdf2 (crypto.js:607:20)
    at Object.exports.pbkdf2Sync (crypto.js:590:10)


    crypto: require("crypto"),

    encrypt: function(password) {
        var salt = this.getSalt(password, this.constructor.GUID);
        return {salt: salt, password: this.getEncrypted(password, salt)};
    },

    getSalt: function(password, GUID) {
        return this.crypto.createHash("sha256").update(password + "|" + GUID).digest("hex")
    },

    getEncrypted: function(password, salt) {
        return this.crypto.pbkdf2Sync(password, salt, 4096, 512, "sha256").toString("hex");
    },

    verifyPassword: function(user, password) { //var salt = this.getSalt("test@test.com|" + this.constructor.GUID); console.log("salt: " + salt); console.log("password: " + this.getPassword("HUG2015", salt));
        return this.crypto.pbkdf2Sync(password, user.salt, 4096, 512, "sha256").toString("hex")  == user.password; //test
    },

    generateAuthToken: function() {
        return this.crypto.randomBytes(64).toString("hex");
    }

Edit: Usage 编辑:用法

        getUser: function(emailAddress, password) {
            var self = this;
            this.connection.query("SELECT * from admin WHERE emailAddress = ?", [emailAddress], function(error, rows, fields){
                if(error) {
                    self.onFault({status: 500, body: error});
                } else {
                    if(rows.length == 1) {
                        self.verifyPassword(rows[0], password)
                    } else {
                        self.onFault({status: 401, body: {}});
                    }
                }
            });
        },

        verifyPassword: function(user, password) {
            var self = this;
            try {
                if(this.authenticationProxy.verifyPassword(user, password)) {
                    this.setAuthToken(user, this.authenticationProxy.generateAuthToken());
                } else {
                    this.onFault({status: 401, body: {}});
                }
            } catch(exception) {
                this.onFault({status:500, body: exception});
            }
        },

My guess is, that 'not a buffer' is thrown if password is a number. 我的猜测是,如果密码是数字,则会抛出“ not buffer”。 Inside crypto.js there is a toBuf call which tries to change password into a buffer (unless it is already a buffer) but converts Strings only. 在crypto.js内部,有一个toBuf调用,该调用尝试将密码更改为缓冲区(除非它已经是缓冲区),但仅转换字符串。

You can try two things: 您可以尝试两件事:

1) ensure that password (passed to crypto.pbkdf2Sync ) is a String 1)确保密码(传递给crypto.pbkdf2Sync )是一个字符串

2) or convert it to buffer yourself -> pass new Buffer(password, 'binary') 2)或将其转换为自己缓冲->传递new Buffer(password, 'binary')

If this code hasn't changed at all and suddenly it's not working, it's because one of the calling libraries or other changes you have made are interfering. 如果此代码根本没有更改,并且突然不起作用,那是因为调用库之一或您所做的其他更改正在干扰。

(Or a new node version is being used, but I doubt that's your problem). (或者正在使用新的节点版本,但是我怀疑那是您的问题)。

The pattern you are using is not recommended: 不建议您使用的模式:

mysingleton = {

crypto: require('crypto'),
...

}

This is because any callback through an anonymous function will lose your 'this' Object. 这是因为通过匿名函数进行的任何回调都将丢失“ this”对象。

Also in node.js, you do not need this pattern, because each file in mode.js is kept in a separate namespace and protected by the module.exports. 同样在node.js中,您不需要此模式,因为mode.js中的每个文件都保存在单独的命名空间中,并受module.exports保护。

You can get rid of the references to 'this' in the following way, as a quick check to see if 'this' is the culprit: 您可以通过以下方式摆脱对“ this”的引用,以快速检查“ this”是否是罪魁祸首:

var
   c = require('crypto');

mysingleton = {

// delete the crypto definition here
// crypto: ...,
...
encrypt: function(password) {
    // note elimination of this below
    var salt = mysingleton.getSalt(password, this.constructor.GUID);
    return {salt: salt, password: this.getEncrypted(password, salt)};
},
...
getSalt: function(password, GUID) {
    return c.createHash("sha256").update(password + "|" + GUID).digest("hex")
},
...
}

If that fixes things, I would suggest you get some more background on what the best recommended pattern is for your framework / system. 如果可以解决问题,我建议您进一步了解最适合您的框架/系统的模式。

In my case, I was trying to send the JSON object directly to the function. 就我而言,我试图将JSON对象直接发送到该函数。

{ token: "abc" }

I stringify the input to the function and everything worked properly. 我将函数的输入字符串化,一切正常。

JSON.stringify({ token: "abc" })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM