gpt4 book ai didi

hash - 每次C#相同的未更改文件的MD5文件哈希均不同

转载 作者:行者123 更新时间:2023-12-04 13:45:26 32 4
gpt4 key购买 nike

大家晚上好
我一直在使用C#的MD5工具进行工作,该工具需要一个文件,经过我的Hasher类,然后将结果以及文件名和目录弹出到数据库中。
我遇到的问题是,每次运行测试时,相同的文件的MD5结果(即在任何方面都没有改变)是完全不同的。
下面是我使用的代码

HashAlgorithm hmacMd5 = new HMACMD5(); 
byte[] hash;
try
{
using (Stream fileStream = new FileStream(fileLocation, FileMode.Open))
{
using (Stream bufferedStream = new BufferedStream(fileStream, 5600000))
{
hash = hmacMd5.ComputeHash(bufferedStream);
foreach (byte x in hash)
{
md5Result += x;
}
}
}
}
catch (UnauthorizedAccessException uae) { }

return md5Result;
以下是hello.mp2的3个单独运行的结果:

1401401571161052548110297623915056204169177

16724366215610475211823021169211793421

56154777074212779619017828183239971


相当令人困惑。
我为什么要得到这些结果的唯一理性思考是将字节连接到字符串。
有人可以在这里发现问题吗?
问候,
里克

最佳答案

您应该改用System.Security.Cryptography.MD5

HMACMD5不计算哈希,而是计算消息身份验证代码。

HMACMD5 is a type of keyed hash algorithm that is constructed from the MD5 hash function and used as a Hash-based Message Authentication Code (HMAC). The HMAC process mixes a secret key with the message data, hashes the result with the hash function, mixes that hash value with the secret key again, then applies the hash function a second time. The output hash will be 128 bits in length



由于您未提供HMAC key ,因此将代表您随机生成一个 key ,并使您看到不同的结果。

关于hash - 每次C#相同的未更改文件的MD5文件哈希均不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1347866/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com