This question already has an answer here:
这个问题已经有了答案:
- How to do Base64 encoding in node.js? 3 answers
- 如何在node.js中进行Base64编码?3答案
I was implementing password hashing with salt, so I generated salt as binary, hashed the password, base64 encoded the password and salt then stored them into database.
我使用salt实现密码散列,因此生成salt作为二进制,散列密码,base64编码密码,salt将它们存储到数据库中。
Now when I am checking password, I am supposed to decode the salt back into binary data, use it to hash the supplied password, base64 encode the result and check if the result matches the one in database.
现在,当我检查密码时,我应该把salt解码回二进制数据,使用它对提供的密码进行散列,base64对结果进行编码,并检查结果是否与数据库中的结果匹配。
The problem is, I cannot find a method to decode the salt back into binary data. I encoded them using the Buffer.toString method but there doesn't seem to be reverse function.
问题是,我找不到一种方法把盐解码成二进制数据。我用缓冲区对它们进行编码。toString方法,但似乎没有反向函数。
1 个解决方案
#1
412
As of Node.js v6.0.0 using the constructor method has been deprecated and the following method should instead be used to construct a new buffer from a base64 encoded string:
的节点。使用构造函数方法已经被弃用,下面的方法应该被用来从base64编码的字符串构造一个新的缓冲区:
var b64string = /* whatever */;
var buf = Buffer.from(b64string, 'base64'); // Ta-da
For Node.js v5.11.1 and below
为节点。js v5.11.1下面
Construct a new Buffer
and pass 'base64'
as the second argument:
构造一个新的缓冲区,并通过'base64'作为第二个参数:
var b64string = /* whatever */;
var buf = new Buffer(b64string, 'base64'); // Ta-da
If you want to be clean, you can check whether from
exists :
如果你想保持清洁,你可以检查是否存在:
if (typeof Buffer.from === "function") {
// Node 5.10+
buf = Buffer.from(b64string, 'base64'); // Ta-da
} else {
// older Node versions
buf = new Buffer(b64string, 'base64'); // Ta-da
}
#1
412
As of Node.js v6.0.0 using the constructor method has been deprecated and the following method should instead be used to construct a new buffer from a base64 encoded string:
的节点。使用构造函数方法已经被弃用,下面的方法应该被用来从base64编码的字符串构造一个新的缓冲区:
var b64string = /* whatever */;
var buf = Buffer.from(b64string, 'base64'); // Ta-da
For Node.js v5.11.1 and below
为节点。js v5.11.1下面
Construct a new Buffer
and pass 'base64'
as the second argument:
构造一个新的缓冲区,并通过'base64'作为第二个参数:
var b64string = /* whatever */;
var buf = new Buffer(b64string, 'base64'); // Ta-da
If you want to be clean, you can check whether from
exists :
如果你想保持清洁,你可以检查是否存在:
if (typeof Buffer.from === "function") {
// Node 5.10+
buf = Buffer.from(b64string, 'base64'); // Ta-da
} else {
// older Node versions
buf = new Buffer(b64string, 'base64'); // Ta-da
}