MD5,SHA-256和SHA-512是否适合计算大型文件的哈希值?

时间:2018-07-05 13:58:53

标签: c# .net md5 sha256 sha512

这是一个应计算文件哈希值的函数。但是我总是不时得到错误的哈希码。

string filePath = @"G:\TV\movie.ts"; // about 10 GByte
private void Test()
{

  for (int i = 0; i < 99; i++)
  {
    byte[] bytes;
    try
    {
      var SHA256 = System.Security.Cryptography.SHA256.Create();
      var MD5 = System.Security.Cryptography.MD5.Create();
      var SHA512 = System.Security.Cryptography.SHA512.Create();
      using (var stream = File.OpenRead(filePath))
      {
        stream.Position = 0;
        bytes = MD5.ComputeHash(stream);
        Debug.Print(i + ": " + BitConverter.ToString(bytes).Replace("-", ""));

        stream.Position = 0;
        bytes = SHA256.ComputeHash(stream);
        Debug.Print(i + ": " + BitConverter.ToString(bytes).Replace("-", ""));

        stream.Position = 0;
        bytes = SHA512.ComputeHash(stream);
        Debug.Print(i + ": " + BitConverter.ToString(bytes).Replace("-", ""));
      }
      MD5.Dispose();
      SHA256.Dispose();
      SHA512.Dispose();
    }
    catch (IOException e)
    {
      Debug.Print(e.ToString());
    }
  }
}

一个大文件(电影〜10GB)的结果。 对于MD5(短)和SHA256(中),我将偏差标记为粗体。 当您查看SHA512(长)结果时,您会发现从执行测试的8倍中,我得到了 4x1B04E14AD6A0D1CC9D131D37D92C90146C118655B50935033A67BC0F436E9CAC424B569CCDB392D12B477DA751F30CD677023D78A55AA3CC97F8F8CD9F941D97 3xF9A6C6C61579FF989F17FB78DD23F1A4B6290588002EE58FB2C9427C75DD35D4745D15DB868CFCC94A49C9227E6C058FB89E862E50E8A06C527FC8C72EDCAFC7 1x1A3050206C8F3E94FC918F140215ADF2A9F463EAF083D421F5AF3FC450A634E9DF04C62889C3F295D504DDE34014CEF4A908C01D26770E250311816F77826B23

这表明,如果出错,通常会导致相同的错误哈希。

所以问题是,为什么每次使用算法算法时都会得到错误的结果?

0:920651841DF511BC45B51DABF76F08D8
0:ADA7EDB960A0EBB298E6BAB8FBF5E2C3D7D2E170B2C19B0B6432A469F2A398B7
0:1B04E14AD6A0D1CC9D131D37D92C90146C118655B50935033A67BC0F436E9CAC424B569CCDB392D12B477DA751F30CD677023D78A55AA3CC97F8F8CD9F941D97

1:920651841DF511BC45B51DABF76F08D8
1:ADA7EDB960A0EBB298E6BAB8FBF5E2C3D7D2E170B2C19B0B6432A469F2A398B7
1:1A3050206C8F3E94FC918F140215ADF2A9F463EAF083D421F5AF3FC450A634E9DF04C62889C3F295D504DDE34014CEF4A908C01D26770E250311816F77826B23

2:920651841DF511BC45B51DABF76F08D8
2:ADA7EDB960A0EBB298E6BAB8FBF5E2C3D7D2E170B2C19B0B6432A469F2A398B7
2:1B04E14AD6A0D1CC9D131D37D92C90146C118655B50935033A67BC0F436E9CAC424B569CCDB392D12B477DA751F30CD677023D78A55AA3CC97F8F8CD9F941D97

3: 944638B455C6C4DBF1B4149B16360C4E
3:ADA7EDB960A0EBB298E6BAB8FBF5E2C3D7D2E170B2C19B0B6432A469F2A398B7
3:F9A6C6C61579FF989F17FB78DD23F1A4B6290588002EE58FB2C9427C75DD35D4745D15DB868CFCC94A49C9227E6C058FB89E862E50E8A06C527FC8C72EDCAFC7

4:920651841DF511BC45B51DABF76F08D8
4:ADA7EDB960A0EBB298E6BAB8FBF5E2C3D7D2E170B2C19B0B6432A469F2A398B7
4:1B04E14AD6A0D1CC9D131D37D92C90146C118655B50935033A67BC0F436E9CAC424B569CCDB392D12B477DA751F30CD677023D78A55AA3CC97F8F8CD9F941D97

5:920651841DF511BC45B51DABF76F08D8
5: 03DD7ADCA595F925E9226D1022D2FFB06F59710677E01472E4267E635DC4020A
5:F9A6C6C61579FF989F17FB78DD23F1A4B6290588002EE58FB2C9427C75DD35D4745D15DB868CFCC94A49C9227E6C058FB89E862E50E8A06C527FC8C72EDCAFC7

6:920651841DF511BC45B51DABF76F08D8
6:ADA7EDB960A0EBB298E6BAB8FBF5E2C3D7D2E170B2C19B0B6432A469F2A398B7
6:1B04E14AD6A0D1CC9D131D37D92C90146C118655B50935033A67BC0F436E9CAC424B569CCDB392D12B477DA751F30CD677023D78A55AA3CC97F8F8CD9F941D97

7: 945F30E57094D2725B727D69F7BE7F4C
7:ADA7EDB960A0EBB298E6BAB8FBF5E2C3D7D2E170B2C19B0B6432A469F2A398B7
7:F9A6C6C61579FF989F17FB78DD23F1A4B6290588002EE58FB2C9427C75DD35D4745D15DB868CFCC94A49C9227E6C058FB89E862E50E8A06C527FC8C72EDCAFC7

1 个答案:

答案 0 :(得分:1)

如某些评论所述,这似乎是硬件问题。重置BIOS后,它似乎可以再次正常工作。