如何在 Node.js 中实现“字符串的 Base64 编码 SHA-1 哈希值”

gre*_*and 5 base64 sha1 node.js

如何在 Nodejs 中实现“字符串的 Base64 编码 SHA-1 哈希”。

谢谢。

小智 5

Crypto可以直接返回base64编码的结果:

var crypto = require('crypto');
var s = 'the quick brown fox';
var sha = crypto.createHash('sha1');
sha.update(s);
var ret = sha.digest('base64');
console.log(ret);
Run Code Online (Sandbox Code Playgroud)


BRo*_*ers -1

您尝试过使用加密货币吗? http://nodejs.org/api/crypto.html

var ciphers = crypto.getCiphers();
console.log(ciphers); // ['AES-128-CBC', 'AES-128-CBC-HMAC-SHA1', ...]

var hashes = crypto.getHashes();
console.log(hashes); // ['sha', 'sha1', 'sha1WithRSAEncryption', ...]
Run Code Online (Sandbox Code Playgroud)

来自网站:

crypto.createHash(algorithm)# 创建并返回一个哈希对象,这是具有给定算法的加密哈希,可用于生成哈希摘要。

算法取决于平台上 OpenSSL 版本支持的可用算法。例如“sha1”、“md5”、“sha256”、“sha512”等。在最近的版本中,openssl list-message-digest-algorithms 将显示可用的摘要算法。

示例:该程序获取文件的 sha1 和

var filename = process.argv[2];

var crypto = require('crypto');
var fs = require('fs');

var shasum = crypto.createHash('sha1');

var s = fs.ReadStream(filename);
s.on('data', function(d) {
  shasum.update(d);
});

s.on('end', function() {
  var d = shasum.digest('hex');
  console.log(d + '  ' + filename);
});
Run Code Online (Sandbox Code Playgroud)