本文介绍了使用DigestInputStream计算来自同一InputStream的多个校验和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图找出如何使用 DigestInputStream $基于相同的
InputStream
读取多个摘要(md5,sha1,gpg) C $ C>。根据我在文档中检查的内容,似乎可以通过克隆摘要来实现。有人可以说明一下吗?
I am trying to figure out how to read multiple digests (md5, sha1, gpg) based on the same InputStream
using DigestInputStream
. From what I've checked in the documentation, it seems to be possible by cloning the digest. Could somebody please illustrate this?
我不想重新读取流来计算校验和。
I don't want to be re-reading the stream in order to calculate the checksums.
推荐答案
您可以围绕 DigestInputStream
包装 DigestInputStream
所以递归:
You could wrap a DigestInputStream
around a DigestInputStream
and so on recursively:
DigestInputStream shaStream = new DigestInputStream(
inStream, MessageDigest.getInstance("SHA-1"));
DigestInputStream md5Stream = new DigestInputStream(
shaStream, MessageDigest.getInstance("MD5"));
// VERY IMPORTANT: read from final stream since it's FilterInputStream
byte[] shaDigest = shaStream.getMessageDigest().digest();
byte[] md5Digest = md5Stream.getMessageDigest().digest();
这篇关于使用DigestInputStream计算来自同一InputStream的多个校验和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!