Vali Moldovan
Vali Moldovan

Reputation: 13

Get hashcode of a svgfile inside a zip

I have a zip archive containing svg files, I need to get the hash of the content of the svg files and store them so on next execution I can check if the file has been modified or not. My problem is that I get different hashes even though the file wasn't modified

using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
  using (HashAlgorithm sha1 = SHA1.Create())
  {
    foreach (var entry in archive.Entries)
    {
      byte[] hash;
      using (GZipStream svgStream = new GZipStream(entry.Open(), CompressionMode.Decompress))
      {
        hash = sha1.ComputeHash(svgStream);
      }

      documents.Add(new BsonDocument("_id", entry.Name).Add("Hash", hash));
    }
  }
}

collection.InsertMany(documents);

This is the script I run for all the files to store the hashes in a database, when I do the same in my application, the hash returned by the same function is different

using (ZipArchive archive = ZipFile.OpenRead(zipFile)){
  foreach(var entry in archive.Entries)
  {
    using (GZipStream svgStream = new GZipStream(entry.Open(), CompressionMode.Decompress))
    {
      var hash = sha1.ComputeHash(svgStream);
      if (hashes.ContainsKey(entry.Name) && hashes[entry.Name].SequenceEqual(hash))
      {
          //code if same hash, never enters here 
      }
      //code if different
   }
 }
}

I am not quite sure if trying to get the hash of this stream is even correct or not

var hash = sha1.ComputeHash(svgStream);

Upvotes: 0

Views: 83

Answers (0)

Related Questions