的NodeJS:如何去code的base64 EN codeD字符串为二进制? [英] NodeJS: How to decode base64 encoded string back to binary?

查看:201
本文介绍了的NodeJS:如何去code的base64 EN codeD字符串为二进制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施的密码与盐散列,所以我生成盐二进制,散列密码的base64 EN codeD密码和盐然后存储到他们的数据库。

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.

现在,当我检查的密码,我应该去code盐回二进制数据,用它来散列提供的密码,基数64 EN code的结果,检查结果相匹配的在数据库中。

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.

问题是,我无法找到脱code盐回二进制数据的方法。我带codeD他们使用Buffer.toString方法,但似乎并没有被逆转。

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.

推荐答案

作为Node.js的V6.0.0 使用的构造方法已去precated 和下面的方法应该改为可用于从一个base64 EN codeD字符串建立一个新的缓冲区:

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:

var b64string = /* whatever */;
var buf = Buffer.from(b64string, 'base64'); // Ta-da

有关的Node.js v5.11.1及以下

构造一个新的缓存'的base64 作为第二个参数:

Construct a new Buffer and pass 'base64' as the second argument:

var b64string = /* whatever */;
var buf = new Buffer(b64string, 'base64'); // Ta-da

这篇关于的NodeJS:如何去code的base64 EN codeD字符串为二进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆