计算大文件MD5SUM在C# [英] Computing MD5SUM of large files in C#
问题描述
我使用下面的代码来计算文件的MD5SUM -
I am using following code to compute MD5SUM of a file -
byte[] b = System.IO.File.ReadAllBytes(file);
string sum = BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(b));
这正常工作正常,但如果我遇到一个大文件(〜1GB) - 例如一个ISO镜像或DVD的VOB文件 - 我得到一个内存异常
This works fine normally, but if I encounter a large file (~1GB) - e.g. an iso image or a DVD VOB file - I get an Out of Memory exception.
不过,我能计算出MD5SUM在Cygwin中为同一个文件中有关10secs
Though, I am able to compute the MD5SUM in cygwin for the same file in about 10secs.
请建议我怎么能得到这个在我的程序的大文件的工作。
Please suggest how can I get this to work for big files in my program.
感谢
推荐答案
我建议使用另一种方法:
I suggest using the alternate method:
MD5CryptoServiceProvider.ComputeHash(Stream)
和只是通过输入流中打开你的文件。这种方法几乎肯定会的不的在一气呵成在内存中读取整个文件。
and just pass in an input stream opened on your file. This method will almost certainly not read in the whole file in memory in one go.
我还要指出,在MD5的大多数实现它的可以添加字节[]
数据放入摘要函数在一个时间块,然后要求在年底的哈希值。
I would also note that in most implementations of MD5 it's possible to add byte[]
data into the digest function a chunk at a time, and then ask for the hash at the end.
这篇关于计算大文件MD5SUM在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!