简体   繁体   English

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

[英]Calculate sha1 hash of a stream but still use the stream?

I have a function which will write/consume a stream like so. 我有一个函数可以像这样写/使用流。

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

But I would like to calculate the SHA1 hash of the stream right before calling this function. 但是我想在调用此函数之前计算流的SHA1哈希。 I know you can get the hash like so: 我知道您可以像这样获得哈希值:

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
})

But every time I try to call the consume function the stream is empty. 但是每次我尝试调用消耗函数时,流都是空的。 How can I do this? 我怎样才能做到这一点?

You could pipe stream to both sha1Calc and consume : 您可以通过管道将stream同时streamsha1Calc consume

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM