nodejs pbkdf2sync 不是缓冲区错误

nodejs pbkdf2sync Not a buffer error

突然在我的控制台中开始出现错误,运行 好几天都没有任何变化。请指教。

/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");
    }

编辑:用法

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

如果此代码根本没有更改,但突然无法正常工作,那是因为调用库之一或您所做的其他更改造成干扰。

(或者正在使用新的节点版本,但我怀疑这是你的问题)。

不推荐您使用的模式:

mysingleton = {

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

}

这是因为通过匿名函数进行的任何回调都会丢失您的 'this' 对象。

同样在 node.js 中,您不需要此模式,因为 mode.js 中的每个文件都保存在单独的命名空间中并受 module.exports.

保护

您可以通过以下方式删除对 '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")
},
...
}

如果这能解决问题,我建议您了解更多背景知识,了解最适合您的框架/系统的推荐模式。

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

您可以尝试两件事:

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

2) 或者自己转换成buffer -> pass new Buffer(password, 'binary')

在我的例子中,我试图将 JSON 对象直接发送到函数。

{ token: "abc" }

我将函数的输入字符串化,一切正常。

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