加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 04:14:44 所属栏目:百科 来源:网络整理
导读:晚上好, 我一直在使用C#中的MD5工具来获取文件,浏览我的Hasher类并将结果弹出数据库,以及文件名和目录. 我遇到的问题是,每次运行测试时,同一个相同文件的MD5结果,即以任何方式保持不变都是完全不同的. 以下是我使用的代码 HashAlgorithm hmacMd5 = new HMACM
晚上好,

我一直在使用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密钥,因此代表您随机生成一个密钥,并使您看到不同的结果.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读