Node.js:crypto.pbkdf2密码为十六进制

bod*_*ser 5 javascript encoding cryptography node.js

我目前使用以下设置来注册新用户:

// creates a new user
app.post('/users', function(req, res) {
    // create new user
    var user = new User();
    // assign post
    user.username = req.body.username;
    user.email = req.body.email;

    crypto.randomBytes(32, function(err, buf) {
        if (err) throw err;
        user.salt = buf.toString('hex');
        crypto.pbkdf2(req.body.password, user.salt, 25000, 512, function(err, encodedPassword) {
            if (err) throw err;
            user.password = (encodedPassword.toString('hex')); // this line
            user.save(function(err, user) {
                if (!err) return res.send(err, 500);
                return res.json(user);
            });
        }.bind(this));
    });
});
Run Code Online (Sandbox Code Playgroud)

仔细看看这一行:

user.password = (encodedPassword.toString('hex'));
Run Code Online (Sandbox Code Playgroud)

这应该将密码字符串(看起来像二进制字符串)编码为十六进制字符串.出于某种原因,这不起作用.

为什么不?

Byside:盐和密码存储的推荐编码是什么(十六进制,二进制,base64)?

pix*_*ort 9

看起来如果它已经是一个String,那么toString('hex')将不起作用.

我做的是什么Buffer(encodedPassword, 'binary').toString('hex').