SHA256、SHA512、MD5 为 C# .NET 6 中的不同文件生成完全相同的哈希值

发布于 2025-01-19 15:56:44 字数 880 浏览 3 评论 0原文

我有一个奇怪的问题。我有以下非常基本的代码:

private async Task<string> _CalculateChecksum(Stream strm)
    {
        System.Security.Cryptography.MD5 sha = System.Security.Cryptography.MD5.Create();
        byte[] checksum = await sha.ComputeHashAsync(strm);
        //return BitConverter.ToString(checksum).Replace("-", "");
        return Convert.ToBase64String(checksum);
    }

我想计算文件的哈希值。我先用 SHA256 尝试过。如果我将哈希算法从 MD5 更改为 SHA256 或 SHA512 并不重要,不同文件生成的哈希值始终相同。例如:其中一个测试文件的大小约为。 4. 比其他文件大。所有算法的哈希值都相同。我还尝试使用 BitConverter.ToString 而不是 Convert.ToBase64String,但这并不重要,因为校验和字节数组的内容完全相同。

我也尝试使用普通(非异步)方法,但结果是相同的。

有谁知道为什么会发生这种情况?

以下是调用哈希部分的代码:

MemoryStream ms = new MemoryStream();
await file.CopyToAsync(ms);
string hash = await _CalculateChecksum(ms);

“file”是一个包含正确值的 IFormFile 对象。

I have a strange issue. I have the following very basic code:

private async Task<string> _CalculateChecksum(Stream strm)
    {
        System.Security.Cryptography.MD5 sha = System.Security.Cryptography.MD5.Create();
        byte[] checksum = await sha.ComputeHashAsync(strm);
        //return BitConverter.ToString(checksum).Replace("-", "");
        return Convert.ToBase64String(checksum);
    }

I would like to calculate hash values for files. I tried it with SHA256 first. It doesn't matter if I change the hash algorithm from MD5 to either SHA256 or SHA512, the generated hash values for different files are always the same. E.g.: one of test file's size is approx. 4. bigger than the other file. The hash is the same with all algorithms. I also tried using BitConverter.ToString instead of Convert.ToBase64String, but it does not matter because the content of checksum byte array is exactly the same.

I also tried using normal (non-async) method, but the result is the same.

Does anyone have any idea why is this happening?

Here is the code where the hashing part is called:

MemoryStream ms = new MemoryStream();
await file.CopyToAsync(ms);
string hash = await _CalculateChecksum(ms);

"file" is an IFormFile object which contains the correct value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人海汹涌 2025-01-26 15:56:44

await file.CopyToAsync(ms)之后,内存流的位置位于其末尾。 sha.ComputeHashAsync(strm) 计算当前位置之后数据的哈希值,因此基本上是空流的哈希值。

尝试在调用 _CalculateChecksum 之前将流的位置重置为开头:

MemoryStream ms = new MemoryStream();
await file.CopyToAsync(ms);
ms.Seek(0, SeekOrigin.Begin);
string hash = await _CalculateChecksum(ms);

After the await file.CopyToAsync(ms), the position of the memory stream is at its end. sha.ComputeHashAsync(strm) calculates the hash of the data after the current position, so basically that of an empty stream.

Try resetting the position of the stream to the start before the call to _CalculateChecksum:

MemoryStream ms = new MemoryStream();
await file.CopyToAsync(ms);
ms.Seek(0, SeekOrigin.Begin);
string hash = await _CalculateChecksum(ms);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文