Node.js:crypto.pbkdf2密码为hex
我目前使用以下设置注册新用户:
// 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)); }); });
仔细看看这一行:
user.password = (encodedPassword.toString('hex'));
这应该将密码string(看起来像二进制)编码成hexstring。 由于某种原因,这是行不通的。
为什么不?
Byside :盐和密码存储(hex,二进制,base64)推荐使用什么编码?
看来,如果它已经是一个string,toString('hex')将无法正常工作。
我所做的就像Buffer(encodedPassword, 'binary').toString('hex')
。