本文介绍了nodejs pbkdf2sync不是缓冲区错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

突然在我的控制台中开始出现错误,并且连续数天运行良好,没有任何更改。请指教。

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("[email protected]|" + 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");
    }

编辑:用法

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


推荐答案

我的猜测是,如果 password 是数字,则会抛出 not buffer。在crypto.js中,有一个 toBuf 调用,该调用试图将 password 更改为缓冲区(除非它已经是缓冲区),但只能转换字符串。

My guess is, that 'not a buffer' is thrown if password is a number. 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.

您可以尝试以下两种操作:

You can try two things:

1)确保密码(传递给 crypto.pbkdf2Sync )是一个字符串

1) ensure that password (passed to crypto.pbkdf2Sync) is a String

2)或将其转换为自己缓冲->传递 new Buffer(password,'binary')

2) or convert it to buffer yourself -> pass new Buffer(password, 'binary')

这篇关于nodejs pbkdf2sync不是缓冲区错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 10:48