相同的不变文件的MD5文件散列每次都是不同的C# [英] MD5 file hash for the same unchanged file is different each time C#

查看:111
本文介绍了相同的不变文件的MD5文件散列每次都是不同的C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好,



我一直在使用C#中的MD5工具,它需要一个文件,通过我的Hasher类并将结果弹出到数据库中,以及文件名和目录。



我遇到的问题是每次运行测试时,MD5结果都是相同的相同文件以任何方式改变是完全不同的。



以下是我使用的代码

  HashAlgorithm hmacMd5 = new HMACMD5(); 
byte [] hash;
试用
{
使用(Stream fileStream = new FileStream(fileLocation,FileMode.Open))
{
using(Stream bufferedStream = new BufferedStream(fileStream,5600000) )
{
hash = hmacMd5.ComputeHash(bufferedStream);
foreach(hash中的字节x)
{
md5Result + = x;



$ b catch(UnauthorizedAccessException uae){}

return md5Result;

以下是hello.mp2的单独运行结果:


<1401401571161052548110297623915056204169177



16724366215610475211823021169211793421


56154777074212779619017828183239971

>

相当令人费解。
我唯一理性的想法是为什么我要得到这些结果是将字节连接到字符串。



任何人都可以在这里找到问题吗?



Regards,

Ric

解决方案

您应该使用 System.Security.Cryptography。 MD5 而不是。



HMACMD5 不计算散列值,它会计算一个消息认证码。


HMACMD5是一种密钥散列
算法,它由
MD5散列函数构造并用作
基于散列的消息认证码
(HMAC)。 HMAC过程将
秘密密钥与消息数据混合,
用哈希
函数对结果进行哈希处理,再将该哈希值与
秘密密钥混合,然后将
哈希函数第二次。
输出散列的长度为128位


由于您没有提供HMAC密钥,因此正在生成一个随机为你代表你并导致你看到不同的结果。


Good evening all,

I've been working on an MD5 tool in C# that takes a file, goes through my Hasher class and pops the result in a database, along with the filename and directory.

The issue I'm having is that each time I run the test, the MD5 result for the same identical file i.e. unchanged in any way is completely different.

Below is the code I use

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;

Here are the results for 3 seperate runs of hello.mp2:

1401401571161052548110297623915056204169177

16724366215610475211823021169211793421

56154777074212779619017828183239971

Quite puzzling. My only rational thought as to why I'm getting these results is with concatenating byte to string.

Can anyone spot an issue here?

Regards,

Ric

解决方案

You should use System.Security.Cryptography.MD5 rather.

HMACMD5 doesn't compute a hash, it computes a message authentication code.

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

Since you're not supplying the HMAC key, one is being generated randomly for you on your behalf and causing you to see different results.

这篇关于相同的不变文件的MD5文件散列每次都是不同的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆