Top

Tags


Roadkill .NET Wiki

Google ads

Recommended reading


Search

Wednesday
Jan142009

C# MD5 and SHA encryption wrapper class 

This is a simple utility class for MD5 (128bit) or SHA-2 (256bit,384bit and 512bit) hash encryption.

It outputs the string using 2 byte hex values, e.g. AB12FE. It doesn't include SHA-1 but that's trivial to add.

public class HashEncryption
{
    /// <summary>
    /// Encrypts a string using the MD5 hash encryption algorithm.
    /// Message Digest is 128-bit and is commonly used to verify data, by checking
    /// the 'MD5 checksum' of the data. Information on MD5 can be found at:
    /// 
    /// http://www.faqs.org/rfcs/rfc1321.html
    /// </summary>
    /// <param name="Data">A string containing the data to encrypt.</param>
    /// <returns>A string containing the string, encrypted with the MD5 hash.</returns>
    public static string MD5Hash(string Data)
    {
        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] hash = md5.ComputeHash( Encoding.ASCII.GetBytes(Data) );

        StringBuilder stringBuilder = new StringBuilder();
        foreach( byte b in hash ) 
        {
            stringBuilder.AppendFormat("{0:x2}", b);
        }
        return stringBuilder.ToString();
    }



    /// <summary>
    /// Computes an MD5 hash for the provided file.
    /// </summary>
    /// <param name="filename">The full path to the file</param>
    /// <returns>A hexadecimal encoded MD5 hash for the file.</returns>
    public static string MD5FileHash(string filename)
    {
        using (FileStream stream = new FileStream(filename,FileMode.Open,FileAccess.Read))
        {
            using (MD5 md5 = new MD5CryptoServiceProvider())
            {
                byte[] hash = md5.ComputeHash(stream);

                StringBuilder builder = new StringBuilder();
                foreach (byte b in hash)
                {
                    builder.AppendFormat("{0:x2}", b);
                }
                return builder.ToString();
            }
        }
    }

    /// <summary>
    /// Encrypts a string using the SHA256 (Secure Hash Algorithm) algorithm.
    /// Details: http://www.itl.nist.gov/fipspubs/fip180-1.htm
    /// This works in the same manner as MD5, providing however 256bit encryption.
    /// </summary>
    /// <param name="Data">A string containing the data to encrypt.</param>
    /// <returns>A string containing the string, encrypted with the SHA256 algorithm.</returns>
    public static string SHA256Hash(string Data)
    {
        SHA256 sha = new SHA256Managed();
        byte[] hash = sha.ComputeHash( Encoding.ASCII.GetBytes(Data) );

        StringBuilder stringBuilder = new StringBuilder();
        foreach( byte b in hash ) 
        {
            stringBuilder.AppendFormat("{0:x2}", b);
        }
        return stringBuilder.ToString();
    }

    /// <summary>
    /// Encrypts a string using the SHA384(Secure Hash Algorithm) algorithm.
    /// This works in the same manner as MD5, providing 384bit encryption.
    /// </summary>
    /// <param name="Data">A string containing the data to encrypt.</param>
    /// <returns>A string containing the string, encrypted with the SHA384 algorithm.</returns>
    public static string SHA384Hash(string Data)
    {
        SHA384 sha = new SHA384Managed();
        byte[] hash = sha.ComputeHash( Encoding.ASCII.GetBytes(Data) );

        StringBuilder stringBuilder = new StringBuilder();
        foreach( byte b in hash ) 
        {
            stringBuilder.AppendFormat("{0:x2}", b);
        }
        return stringBuilder.ToString();
    }


    /// <summary>
    /// Encrypts a string using the SHA512 (Secure Hash Algorithm) algorithm.
    /// This works in the same manner as MD5, providing 512bit encryption.
    /// </summary>
    /// <param name="Data">A string containing the data to encrypt.</param>
    /// <returns>A string containing the string, encrypted with the SHA512 algorithm.</returns>
    public static string SHA512Hash(string Data)
    {
        SHA512 sha = new SHA512Managed();
        byte[] hash = sha.ComputeHash( Encoding.ASCII.GetBytes(Data) );

        StringBuilder stringBuilder = new StringBuilder();
        foreach( byte b in hash ) 
        {
            stringBuilder.AppendFormat("{0:x2}", b);
        }
        return stringBuilder.ToString();
    }
}

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
« C# Design Patterns: the Iterator pattern | Main | C# Design Patterns: the Singleton pattern »