我需要计算相当大的文件(千兆字节)的校验和.这可以使用以下方法完成:
private byte[] calcHash(string file)
{
System.Security.Cryptography.HashAlgorithm ha = System.Security.Cryptography.MD5.Create();
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] hash = ha.ComputeHash(fs);
fs.Close();
return hash;
}
Run Code Online (Sandbox Code Playgroud)
但是,文件通常是以缓冲方式预先写入的(比如一次写入32mb).我确信我看到了一个覆盖哈希函数,它允许我在写入的同时计算MD5(或其他)哈希,即:计算一个缓冲区的哈希值,然后将得到的哈希值提供给下一次迭代.
像这样的东西:(伪代码)
byte [] hash = new byte [] { 0,0,0,0,0,0,0,0 };
while(!eof)
{
buffer = readFromSourceFile();
writefile(buffer);
hash = calchash(buffer, hash);
}
Run Code Online (Sandbox Code Playgroud)
hash现在通过在整个文件上运行calcHash函数来实现.
现在,我无法在.Net 3.5框架中找到任何覆盖,我在做什么?它从来没有存在过,或者我只是在搜索时很糟糕?同时进行写入和校验和计算的原因是因为大文件有意义.
问题:
"如果您尝试使用包含大文件的缓冲区使用metod HashData(IBuffer)在Windows 8 Metro App中计算md5或sha,则会收到OutOfMemoryException,因为缓冲区非常大(包含原始字节中的副本)文件)."
解:
//NB: "file" is a "StorageFile" previously openedHashAlgorithmProvider md5 = Windows.Security.Cryptography.Core.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
//in this example I use HashAlgorithmNames.Md5, you can replace it with HashAlgorithmName.Sha1, etc...
HashAlgorithmProvider alg = Windows.Security.Cryptography.Core.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
var stream = await file.OpenStreamForReadAsync();
var inputStream = stream.AsInputStream();
uint capacity = 100000000;
Windows.Storage.Streams.Buffer buffer = new Windows.Storage.Streams.Buffer(capacity);
var hash = alg.CreateHash();
while (true)
{
await inputStream.ReadAsync(buffer, capacity, InputStreamOptions.None);
if (buffer.Length > 0)
hash.Append(buffer);
else
break;
}
string hashText = CryptographicBuffer.EncodeToHexString(hash.GetValueAndReset()).ToUpper();
inputStream.Dispose();
stream.Dispose();
Run Code Online (Sandbox Code Playgroud)
我希望这有用:)