MD5, SHA1, SHA256 and SHA512 Hash Algorithm Functions in C#

Published by Utkarsh Patel on 11/05/09 23:42:46
Last edited on 12/05/09 00:08:05

One of the great problems I have had with OO languages such as .NET and Java is that some tasks, that are as simple as calling a function in other languages, are complicated to no end. One such problem is with the different hash functions in .NET available via System.Security.Cryptography. In PHP if I need to get the MD5 of a string I simply call the built in MD5 function. In .NET I need to build my own MD5 function using System.Security.Cryptography and that, even though I understand why it is the way it is, can get a little annoying after a while. I have taken the liberty of simplifying this process by creating a HASH class that can be plugged into your programs as either a new class or by creating a hash.dll of out of it. The code lets you call MD5, SHA1, SHA256 and SHA512 functions and takes care of the process for you.

using System;
using System.Security.Cryptography;
using System.Text;

namespace Hash
{
    public class Hash
    {
        public Hash() { }

        public enum HashType : int
        {
            MD5,
            SHA1,
            SHA256,
            SHA512
        }

        public static string GetHash(string text, HashType hashType)
        {
            string hashString;
            switch (hashType)
            {
                case HashType.MD5: 
                    hashString = GetMD5(text); 
                    break;
                case HashType.SHA1: 
                    hashString = GetSHA1(text); 
                    break;
                case HashType.SHA256:
                    hashString = GetSHA256(text);
                    break;
                case HashType.SHA512:
                    hashString = GetSHA512(text);
                    break;
                default: 
                    hashString = "Invalid Hash Type"; 
                    break;
            }
            return hashString;
        }
        
        public static bool CheckHash(string original, string hashString, HashType hashType)
        {
            string originalHash = GetHash(original, hashType);
            return (originalHash == hashString);
        }

        private static string GetMD5(string text)
        {
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] hashValue;
            byte[] message = UE.GetBytes(text);

            MD5 hashString = new MD5CryptoServiceProvider();
            string hex = "";

            hashValue = hashString.ComputeHash(message);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
            return hex;
        }

        private static string GetSHA1(string text)
        {
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] hashValue;
            byte[] message = UE.GetBytes(text);

            SHA1Managed hashString = new SHA1Managed();
            string hex = "";

            hashValue = hashString.ComputeHash(message);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
            return hex;
        }

        private static string GetSHA256(string text)
        {
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] hashValue;
            byte[] message = UE.GetBytes(text);
            
            SHA256Managed hashString = new SHA256Managed();
            string hex = "";

            hashValue = hashString.ComputeHash(message);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
            return hex;
        }

        private static string GetSHA512(string text)
        {
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] hashValue;
            byte[] message = UE.GetBytes(text);
            
            SHA512Managed hashString = new SHA512Managed();
            string hex = "";

            hashValue = hashString.ComputeHash(message);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
            return hex;
        }
    }
}

Use the GetHash(string,HashType) method in your code to retrieve the hash value of your string, passing in the string and hash type you wish to use. To compare a string and a stored hash value use the CheckHash(string,string,HashType) method, passing in the original string, stored hash string and hash type.

Download Source Code
About the Author

Utkarsh Patel is one of our newest members, and he could not have come at a better time. He has helped bring back stability and control to Techlicity Ventures during our rapid growth. If he isn't marvelling at a recently discovered Oracle feature or technique he is bothering the rest of the team on why we should move everything over to Oracle databases. Utkarsh complements the team well with his determination and sound research.

Bookmark and Share
Blog Widget by LinkWithin
blog comments powered by Disqus

Valid XHTML 1.0!