简体   繁体   English

同一文件不同的md5哈希

[英]Same file different md5 hashes

I have a winforms application where I have option of upload a .csv file. 我有一个winforms应用程序,可以选择上传.csv文件。 I am computing the file hash for the uploaded .csv file. 我正在计算上载的.csv文件的文件哈希。 If the contents of the file has changed then I display a message box saying the data of the file has changed. 如果文件的内容已更改,那么我会显示一个消息框,指出文件的数据已更改。

I know how to compute the file hashes, but kind of stuck with how to store the old file hash of the same file and then comparing it with the new file hash of the same file. 我知道如何计算文件哈希值,但是有点受制于如何存储相同文件的旧文件哈希值,然后将其与相同文件的新文件哈希值进行比较。

static bool FileHashesAreEqual(FileInfo fileName)
{
     byte[] firstHash = MD5.Create().ComputeHash(firstName.OpenRead());
     var oldFileHash = firstHash;

     for (int i = 0; i < oldFileHash.Length; i++)
     {
         // Unable to figure out how to compare newFileHash and the oldFileHash
         //if (oldFileHash[i] != newFileHash[i])
         return false;
     }

     return true;
 }

Any help on how to do this is really appreciated. 非常感谢您提供有关如何执行此操作的帮助。

I got it to work. 我知道了。 Thanks everyone for your ideas and comments. 感谢大家的想法和意见。

private bool FileHashesAreEqual(FileInfo fileName)
{
    byte[] firstHash = MD5.Create().ComputeHash(fileName.OpenRead());

    if (!this.fileHashDictionary.ContainsKey(fileName.Name))
    {
        this.fileHashDictionary.Add(fileName.Name, firstHash.ToString());
    }
    else
    {
        if (this.fileHashDictionary.TryGetValue(fileName.Name, out var value))
        {
            if (value != null)
            {
               for (var index = 0; index < value.Length; index++)
               {
                   if (value[index] != firstHash[index])
                   {
                       return false;
                   }
                }
             }
         }
    }

    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM