简体   繁体   中英

Same file different md5 hashes

I have a winforms application where I have option of upload a .csv file. I am computing the file hash for the uploaded .csv file. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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