将 SHA-256 与 NodeJS 加密结合使用
Using SHA-256 with NodeJS Crypto
我正在尝试像这样在 NodeJS 中散列变量:
var crypto = require('crypto');
var hash = crypto.createHash('sha256');
var code = 'bacon';
code = hash.update(code);
code = hash.digest(code);
console.log(code);
但看起来我误解了文档,因为 console.log 没有记录 bacon 的散列版本,而是记录了一些关于 SlowBuffer 的信息。
正确的做法是什么?
base64:
const hash = crypto.createHash('sha256').update(pwd).digest('base64');
十六进制:
crypto.createHash('sha256').update(pwd).digest('hex');
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
hash.on('readable', () => {
const data = hash.read();
if (data) {
console.log(data.toString('hex'));
// Prints:
// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
}
});
hash.write('some data to hash');
hash.end();
与上面的答案类似,但这显示了如何进行多次写入;例如,如果您从文件中逐行读取,然后将每一行作为单独的操作添加到哈希计算中。
在我的示例中,我还 trim 换行/跳过空行(可选):
const {createHash} = require('crypto');
// lines: array of strings
function computeSHA256(lines) {
const hash = createHash('sha256');
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim(); // remove leading/trailing whitespace
if (line === '') continue; // skip empty lines
hash.write(line); // write a single line to the buffer
}
return hash.digest('base64'); // returns hash as string
}
我使用此代码确保生成的文件行不会被人手动编辑。为此,我写下这些行,在 sha265-sum 后附加一行 sha256:<hash>
,然后,在下一个 运行 上,验证这些行的散列是否与所述 sha265-sum 匹配。
你可以这样使用,在这里创建一个重置令牌(resetToken),这个令牌用于创建一个十六进制的version.in数据库,你可以存储十六进制版本。
// Generate token
const resetToken = crypto.randomBytes(20).toString('hex');
// Hash token and set to resetPasswordToken field
this.resetPasswordToken = crypto
.createHash('sha256')
.update(resetToken)
.digest('hex');
console.log(resetToken )
我正在尝试像这样在 NodeJS 中散列变量:
var crypto = require('crypto');
var hash = crypto.createHash('sha256');
var code = 'bacon';
code = hash.update(code);
code = hash.digest(code);
console.log(code);
但看起来我误解了文档,因为 console.log 没有记录 bacon 的散列版本,而是记录了一些关于 SlowBuffer 的信息。
正确的做法是什么?
base64:
const hash = crypto.createHash('sha256').update(pwd).digest('base64');
十六进制:
crypto.createHash('sha256').update(pwd).digest('hex');
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
hash.on('readable', () => {
const data = hash.read();
if (data) {
console.log(data.toString('hex'));
// Prints:
// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
}
});
hash.write('some data to hash');
hash.end();
与上面的答案类似,但这显示了如何进行多次写入;例如,如果您从文件中逐行读取,然后将每一行作为单独的操作添加到哈希计算中。
在我的示例中,我还 trim 换行/跳过空行(可选):
const {createHash} = require('crypto');
// lines: array of strings
function computeSHA256(lines) {
const hash = createHash('sha256');
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim(); // remove leading/trailing whitespace
if (line === '') continue; // skip empty lines
hash.write(line); // write a single line to the buffer
}
return hash.digest('base64'); // returns hash as string
}
我使用此代码确保生成的文件行不会被人手动编辑。为此,我写下这些行,在 sha265-sum 后附加一行 sha256:<hash>
,然后,在下一个 运行 上,验证这些行的散列是否与所述 sha265-sum 匹配。
你可以这样使用,在这里创建一个重置令牌(resetToken),这个令牌用于创建一个十六进制的version.in数据库,你可以存储十六进制版本。
// Generate token
const resetToken = crypto.randomBytes(20).toString('hex');
// Hash token and set to resetPasswordToken field
this.resetPasswordToken = crypto
.createHash('sha256')
.update(resetToken)
.digest('hex');
console.log(resetToken )