Using CRC32 to compare two files

In the last post, I wrote about using MD5 to compare two files.


 Another way to calculate the has value is using Crc32. Unfortunately .Net
 does not have an implemntation of this Algo. But the open source Zip utility
 for .NET,
SharpZipLib has this utility. Here are the same files compared
 using Crc32 using the Crc32 class from the 
 ICSharpCode.SharpZipLib.Checksums namespace.

   

    1  Crc32 crc32 = new Crc32();

    2             FileStream fileStream_1 =

    3                 new FileStream("Test.txt", FileMode.Open);

    4             fileStream_1.Position = 0;

    5             byte[] buffer = GetBuffer(fileStream_1);

    6             crc32.Update(buffer);

    7             long hashValue_1 = crc32.Value;

    8             Console.Out.WriteLine("crc32 = {0}", crc32.Value);

    9             fileStream_1.Close();

   10 

   11             crc32.Reset();

   12 

   13             FileStream fileStream_2 =

   14                 new FileStream("Copy of Test.txt", FileMode.Open);

   15             fileStream_2.Position = 0;

   16             buffer = GetBuffer(fileStream_2);

   17             crc32.Update(buffer);

   18             long hashValue_2 = crc32.Value;

   19             Console.Out.WriteLine("crc32 = {0}", crc32.Value);

   20             fileStream_2.Close();

   21 

   22             Console.Out.WriteLine("hashValue_2.Equals(hashValue_1 = {0}",

   23                 hashValue_2.Equals(hashValue_1));

   24             Console.Read();


where  GetBuffer() is

    1        private static byte[] GetBuffer(Stream stream)

    2         {

    3             byte[] buffer = new byte[(int)stream.Length];

    4             stream.Read(buffer, 0, buffer.Length);

    5             return buffer;

    6         }


The output is



Hope this helps someone

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.