计算流的sha1哈希值,但仍使用该流

Calculate sha1 hash of a stream but still use the stream?

本文关键字:sha1 哈希值 计算      更新时间:2023-09-26

我有一个函数,它将像这样写入/消费流。

consume(stream, function(e,d){});

但是我想在调用这个函数之前计算流的SHA1哈希值。我知道你可以得到这样的散列:

var crypto = require('crypto');
var hash = crypto.createHash('sha1');
stream.on('data', function (data) {
    hash.update(data, 'utf8')
})
stream.on('end', function () {
    hash.digest('hex');c
})

但是每次我尝试调用消费函数时,流都是空的。我该怎么做呢?

您可以将stream管道到sha1Calc consume:

stream.pipe(sha1Calc);
stream.pipe(consume);