最佳答案
在 https://stackoverflow.com/a/18658613/779159中有一个例子,说明如何使用内置的加密库和流来计算文件的 md5。
var fs = require('fs');
var crypto = require('crypto');
// the file you want to get the hash
var fd = fs.createReadStream('/some/file/name.txt');
var hash = crypto.createHash('sha1');
hash.setEncoding('hex');
fd.on('end', function() {
hash.end();
console.log(hash.read()); // the desired sha1sum
});
// read all file and pipe it (write it) to the hash object
fd.pipe(hash);
但是,是否有可能将其转换为使用 ES8异步/等待,而不是使用上面看到的回调,同时仍然保持使用流的效率?