Title: Calculate hash codes for a file in C#
Hash codes are codes that concisely represent files or other chunks of data. The basic idea is to combine the data's bytes in ways so that two different files are likely to have different hash codes. If you save a file's hash code, you can tell whether someone has modified the file by calculating a new hash code and seeing if it matches the value you originally saved.
This example, uses two different hashing algorithms: MD5 and SHA256. Both are provided by the System.Security.Cryptography namespace so the program includes the following using directive.
The following code shows how the program calculates a file's SHA256 hash code.
// The cryptographic service provider.
private SHA256 Sha256 = SHA256.Create();
// Compute the file's hash.
private byte GetHashSha256(string filename)
using (FileStream stream = File.OpenRead(filename))
The program declares an SHA256 object at the class level. (You could create this object inside the GetHashSha256 method, but this version creates it at the class level so it only needs to do it once even if you calculate hash codes for many files.)
The GetHashSha256 method calculates the file's hash code. It opens the file for reading and creates a FileStream associated with it. It then passes the stream to the SHA256 object's ComputeHash method and returns the result.
The code to get hash codes using the MD5 algorithm is exactly the same as the code for the SHA256 algorithm. Just replace "SHA256" with "MD5" in the previous code.
The ComputeHash method returns a byte stream. The program can't display a byte stream directly to the user, so it uses the following method to convert the bytes into a string.
// Return a byte array as a sequence of hex values.
public static string BytesToString(byte bytes)
string result = "";
foreach (byte b in bytes) result += b.ToString("x2");
This code loops through the array's bytes and uses ToString to convert each byte into a two-digit hexadecimal value. It concatenates the values and returns the result.
The rest is easy. The following code shows how the program displays the selected file's hash codes.
// Compute the file's hash code.
private void btnHash_Click(object sender, EventArgs e)
txtMd5.Text = BytesToString(GetHashMD5(txtFile.Text));
txtSha256.Text = BytesToString(GetHashSha256(txtFile.Text));
This code simply calls the GetHashMD5 and GetHashSHA256 methods, passes the results to BytesToString to convert the hash codes into strings, and displays the results in read-only text boxes.
Download the example to experiment with it and to see additional details.