C# – Get a file’s checksum using any hashing algorithm

This article shows how to get a file’s checksum using any of these hashing algorithms: MD5, SHA1, SHA256, SHA384, and SHA512.

If you are only interested getting a specific type of checksum, take a look at the first section. If you’re interested in a general-purpose checksum method that allows you to generate the checksum using any of the five hashing algorithms, skip to the bottom section.

Get a file’s MD5 checksum

Let’s say you’re only interested in getting a specific type of checksum, such as MD5. This section shows how to do that.

You can use System.Security.Cryptography.MD5 to get a file’s MD5 checksum, like this:

public static string GetMD5Checksum(string filename)
{
	using (var md5 = System.Security.Cryptography.MD5.Create())
	{
		using (var stream = System.IO.File.OpenRead(filename))
		{
			var hash = md5.ComputeHash(stream);
			return BitConverter.ToString(hash).Replace("-", "");
		}
	}
}
Code language: C# (cs)

This generates the MD5 checksum, converts to a hex string, and removes dash characters. This is the typical format for MD5 checksums.

Use this on a file like this:

static void Main(string[] args)
{
	var checksum = GetMD5Checksum(@"C:\NFLTeamStats.json");

	Console.WriteLine(checksum);
}
Code language: C# (cs)

This output’s the file’s MD5 checksum.

AE34D271ACC9C242BC9EED2E0EA72093Code language: plaintext (plaintext)

Get checksum using any hashing algorithm

You can use System.Security.Cryptography.HashAlgorithm.Create() and specify the hashing method you want. This is a factory method that creates an instance of a hashing algorithm object. If you specify “MD5” it creates an System.Security.Cryptography.MD5 object.

This is pretty unsafe though, because you can pass any string to HashAlgorithm.Create(). If that algorithm doesn’t exist, it’ll return a null object.

It’s better to create a wrapper around this and create an enum for specifying the hashing algorithm name. This ensures the caller is passing in a legitimate hashing algorithm.

Here’s the general-purpose GetChecksum() method that accepts a HashingAlgoTypes enum parameter:

public static class ChecksumUtil
{
	public static string GetChecksum(HashingAlgoTypes hashingAlgoType, string filename)
	{
		using (var hasher = System.Security.Cryptography.HashAlgorithm.Create(hashingAlgoType.ToString()))
		{
			using (var stream = System.IO.File.OpenRead(filename))
			{
				var hash = hasher.ComputeHash(stream);
				return BitConverter.ToString(hash).Replace("-", "");
			}
		}
	}

}
public enum HashingAlgoTypes
{
	MD5,
	SHA1,
	SHA256,
	SHA384,
	SHA512
}
Code language: C# (cs)

This generates a checksum using the specified hashing algorithm, converts to a hex string, and removes dashes.

Call it like this:

static void Main(string[] args)
{
	var checksum = ChecksumUtil.GetChecksum(HashingAlgoTypes.SHA512, @"C:\NFLTeamStats.json");

	Console.WriteLine(checksum);
}
Code language: C# (cs)

This output’s the file’s SHA512 checksum:

126C63E7A1C220C8C34D852E861F7A5CADD49B4D98F15C52095F9E5754F2F45BBE363432FDAE2D94EE8CBE7069A90703785C7ECB439534468780A6BBDE6CB06ACode language: plaintext (plaintext)

1 thought on “C# – Get a file’s checksum using any hashing algorithm”

Leave a Comment