9.11 Test Two Files for Equality


Problem

You need to quickly compare the content of two files, and determine if it matches exactly.

Solution

Calculate the hash code of each file using the System.Security.Cryptography.HashAlgorithm class, and compare the hash codes.

Discussion

There are a number of ways you might want to compare more than one file. For example, you could examine a portion of the file for similar data, or you could read through each file byte by byte, comparing each byte as you go. Both of these approaches are valid, but in some cases it's more convenient to use a hash code algorithm.

A hash code algorithm generates a small (typically about 20 bytes) binary fingerprint for a file. While it's possible for different files to generate the same hash codes, that's statistically unlikely to occur. In fact, even a minor change (for example, modifying a single bit in the source file) has an approximately 50 percent chance of independently changing each bit in the hash code. For this reason, hash codes are often used in security code to detect data tampering. (Hash codes are discussed in more detail in Chapter 14.)

To create a hash code, you must first create a HashAlgorithm object, typically by calling the static HashAlgorithm.Create method. You can then call HashAlgorithm.ComputeHash , which returns a byte array with the hash data.

The following code demonstrates a simple Console application that reads two file names that are supplied as arguments and tests the files for equality.

 using System; using System.IO; using System.Security.Cryptography; public class CompareFiles {     private static void Main(string[] args) {         if (args.Length != 2) {             Console.WriteLine("USAGE:  CompareFiles [ file name] [ file name]");             return;         }             Console.WriteLine("Comparing " + args[0] + " and " + args[1]);         // Create the hashing object.         HashAlgorithm hashAlg = HashAlgorithm.Create();         // Calculate the hash for the first file.         FileStream fsA = new FileStream(args[0], FileMode.Open);         byte[] hashBytesA = hashAlg.ComputeHash(fsA);         fsA.Close();         // Calculate the hash for the second file.         FileStream fsB = new FileStream(args[1], FileMode.Open);         byte[] hashBytesB = hashAlg.ComputeHash(fsB);         fsB.Close();         // Compare the hashes.         if (BitConverter.ToString(hashBytesA) ==             BitConverter.ToString(hashBytesB)) {             Console.WriteLine("Files match.");         }else {             Console.WriteLine("No match.");         }                      Console.ReadLine();     } } 

The hashes are compared by converting them into strings. Alternatively, you could compare them by iterating over the byte array and comparing each value. This approach would be slightly faster, but because the overhead of converting 20 bytes into a string is minimal, it's not required.




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net