Java中大文件的MD5或SHA哈希 [英] MD5 or SHA hash of a large file in Java

查看:163
本文介绍了Java中大文件的MD5或SHA哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算一个大文件的SHA或MD5散列。我需要做的是这样的,我举个例子:假设我有一个1GB的文件。
为了计算哈希值,我想把这个文件分成4个部分,分为256MB(我想把它分成小部分)。

1)开始计算第一部分的散列

2)获得第一部分的输出散列,在第二部分的输入中给出它并计算第二部分的散列 p>

3)得到2)点的输出散列值,用第三部分输入并计算第三部分的散列值

4)得到3)点的输出散列值,在第四部分输入它,并计算整个文件的散列值。



我在谷歌搜索了很多,但我还没有发现任何类似的东西。我通过使用方法更新阅读MessageDigest更新我可以读取文件块,直到整个文件加载,然后计算哈希,但我还没有明白,如果当我加载一个大块它也计算它的散列(所以这对我来说很好)或者它只是一个加载并且在末尾计算散列该文件是完全加载的。

解决方案

您可能还想看看 java.security.DigestInputStream 来计算整个文件的散列值。



如果你需要零件的中间散列值,你可以用 java.security.MessageDigest.clone();在部分克隆用于哈希计算的MessageDigest实例并在克隆上调用 digest()以获得哈希值。



原始MessageDigest实例可以用下一部分的字节进行更新。


I need to calculate SHA or MD5 hash of a large file. The thing I need to do is this,I make an example:

Suppose I have a 1GB file. To calculate the hash I want to divide this file in 4 parts of 256MB(I want to divide it in smaller parts).

1)start to calculate the hash of the first part

2)get the output hash of the first part,give it in input with the second part and calculate the hash of the second part

3)get the output hash of the 2) point,give it in input with the third part and calculate the hash of the third part

4)get the output hash of the 3) point,give it in input with the fourth part and calculate the hash of the entire file.

I searched a lot on google but I haven't found nothing similar.I read that with MessageDigest by using the method update I can read file chunks until the entire file is loaded and then calculate the hash but I haven't understood if when I load a chunk it calculate also the hash of it(so it would be good in my case) or if it's only a loading and the hash is calculated at the end when the file is entirely loaded.

解决方案

You may also want to look at java.security.DigestInputStream to calculate the hash of the whole file.

If you need intermediate hashes for the parts, you may have luck with java.security.MessageDigest.clone(); after a part clone the MessageDigest instance used for the hash calculation and call digest() on the clone to get the hash value after the part.

The original MessageDigest instance can be updated with the bytes of the next part.

这篇关于Java中大文件的MD5或SHA哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆