14.3 Calculate the Hash Code of a File


Problem

You need to determine if the contents of a file have changed over time.

Solution

Create a cryptographic hash code of the file's contents using the ComputeHash method of the System.Security.Cryptography.HashAlgorithm class. Store the hash code for future comparison against newly generated hash codes.

Discussion

As well as allowing you to store passwords securely (discussed in recipe 14.2), hash codes provide an excellent means of determining if a file has changed. By calculating and storing the cryptographic hash of a file, you can later recalculate the hash of the file to determine if the file has changed in the interim. A hashing algorithm will produce a very different hash code even if there is a very small change to the file, and the chances of two different files resulting in the same hash code are extremely small.

Warning  

Standard hash codes aren't suitable for sending with a file to ensure the integrity of the file's contents. If someone intercepts the file in transit, that person can easily change the file and recalculate the hash code, leaving the recipient none the wiser. We discuss a variant of the hash code ”a keyed hash code ”in recipe 14.5 that's suitable for ensuring the integrity of a file in transit.

The HashAlgorithm class makes it easy to generate the hash code of a file. Firstly, instantiate one of the concrete hashing algorithm implementations derived from the HashAlgorithm class. To instantiate the desired hashing algorithm class, pass the name of the hashing algorithm to the HashAlgorithm.Create method. See Table 14.1 for a list of valid hashing algorithm names . Then, instead of passing a byte array to the ComputeHash method, you pass a System.IO.Stream object representing the file from which you want to generate the hash code. The HashAlgorithm object handles the process of reading data from the Stream and returns a byte array containing the hash code for the file.

The HashStreamExample class listed here demonstrates the generation of a hash code from a file. You must specify the name of the hashing algorithm and the name of the file as command line arguments, for example HashStreamExample SHA1 HashStreamExample.cs . The example displays the generated hash code to the console.

 using System; using System.IO; using System.Security.Cryptography; public class HashStreamExample {     public static void Main(string[] args) {         // Create a HashAlgorithm of the type specified by the first         // command line argument.         using (HashAlgorithm hashAlg = HashAlgorithm.Create(args[0])) {             // Open a FileStream to the file specified by the second             // command line argument.             using (Stream file = new FileStream(args[1], FileMode.Open)) {                      // Generate the hash code of the file's contents.                 byte[] hash = hashAlg.ComputeHash(file);                      // Display the hash code of the file to the console.                 Console.WriteLine(BitConverter.ToString(hash));             }         }     } } 



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