
Create Hashes: MD5, SHA1, SHA256, SHA384, SHA512
Originally published on my old blog at cumps.be on February 28, 2004.
Today I’ll talk about hash functions.
A hash algorithm takes a piece of text and gives a hash as result. This hash is practically unique for the given text. If you use the hash function on the same text again, you’ll get the same hash. But there is no way to get the given text from the hash. This is great for storing passwords.
Now, in PHP it’s as easy as using md5() or sha1(). But in C# it takes a bit more work. This is what we want to simplify.
So we’ll create a Hash class to create hashes.
Create a new project and add a class (Hash).
using System;
using System.Security.Cryptography;
using System.Text;
namespace Hash {
public class Hash {
public Hash() { }
} /* Hash */
} /* Hash */
Let’s start by adding an enum representing all the hash functions we are going to support.
public enum HashType :int { MD5, SHA1, SHA256, SHA384, SHA512 }
Our class will have 2 public methods, one for creating a hash and one for checking a hash against a given text.
First we’ll create the GetHash method:
public static string GetHash(string strPlain, HashType hshType) {
string strRet;
switch (hshType) {
case HashType.MD5: strRet = GetMD5(strPlain); break;
case HashType.SHA1: strRet = GetSHA1(strPlain); break;
case HashType.SHA256: strRet = GetSHA256(strPlain); break;
case HashType.SHA384: strRet = GetSHA384(strPlain); break;
case HashType.SHA512: strRet = GetSHA512(strPlain); break;
default: strRet = "Invalid HashType"; break;
}
return strRet;
} /* GetHash */
And our CheckHash will depend on this so we might as well add it now.
public static bool CheckHash(string strOriginal, string strHash, HashType hshType) {
string strOrigHash = GetHash(strOriginal, hshType);
return (strOrigHash == strHash);
} /* CheckHash */
As you can see, I created 5 separate methods to create hashes. I’ll explain one for this article, the others are the same but use another hashing class. You’ll find them in the source at the end of this article.
A hash function works on a byte array, so we will create two arrays, one for our resulting hash and one for the given text.
UnicodeEncoding UE = new UnicodeEncoding();
byte[] HashValue, MessageBytes = UE.GetBytes(strPlain);
Now we create an object that will hash our text:
SHA1Managed SHhash = new SHA1Managed();
And finally we calculate the hash and convert it to a hexadecimal string, which we can store in a database, for example.
string strHex = "";
HashValue = SHhash.ComputeHash(MessageBytes);
foreach(byte b in HashValue) {
strHex += String.Format("{0:x2}", b);
}
return strHex;
This is how we test it:
static void Main(string[] args) {
String hash = Hash.Hash.GetHash("This is a sample text :p", Hash.Hash.HashType.SHA256);
Console.WriteLine(hash);
Console.WriteLine(Hash.Hash.CheckHash("This is a sample text :p", hash, Hash.Hash.HashType.SHA256));
Console.WriteLine(Hash.Hash.CheckHash("This is a wrong text.", hash, Hash.Hash.HashType.SHA256));
} /* Main */
Now we have our sweet and simple methods available as a class, ready to be used in any project.
I’ve uploaded the sources again, you will see I documented it as well. When you use NDoc on the generated .xml file you get very sweet MSDN-like documentation.
Update: to get the same result as the md5() function in PHP, use ASCIIEncoding:
ASCIIEncoding UE = new ASCIIEncoding();
Update: MD5 and SHA-1 are no longer considered secure, and plain fast hashes aren’t suitable for passwords; use a dedicated password hashing function such as bcrypt, scrypt, Argon2 or PBKDF2.
You May Also Like

Associating Your Program with Every File: ShellExtension
A C# class that adds your program to the right-click menu of every file, by writing a shell command key under HKEY_CURRENT_USER.

C# Script
CS-Script: a small tool that compiles and runs a single .cs file in memory, like a batch script for C#.

Including Resources in .exe
Embedding the Non-Rectangular Form's bitmap in the .exe as a resource, and loading it with one line of C#.



