Published Tuesday, May 30, 2006 12:50 PM by Aleph

Hash Provider

When dealing with message into an application, and the message that you are receiving or sending is over a not trusted channel you should think about a way to make sure that the message has not been altered. This is why cryptographic hash functions are used.
More on cryptographic hash function you can find here.
Here is a function in C# that can generate for you the hash of a string based on your favorite algorithm.

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

/// <summary>
/// Hash algorithms
/// </summary>
public enum HashType : int
{
    SHA1,
    SHA256,
    SHA384,
    SHA512,
    MD5,
    RIPEMD160
}

public static class Hash
{
    public static string FromString(string input, HashType hashtype)
    {
        Byte[] clearBytes;
        Byte[] hashedBytes;
        string output = String.Empty;

        switch (hashtype)
        {
            case HashType.RIPEMD160:
                clearBytes = new UTF8Encoding().GetBytes(input);
                RIPEMD160 myRIPEMD160 = RIPEMD160Managed.Create();
                hashedBytes = myRIPEMD160.ComputeHash(clearBytes);
                output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
                break;
            case HashType.MD5:
                clearBytes = new UTF8Encoding().GetBytes(input);
                hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
                output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
                break;
            case HashType.SHA1:
                clearBytes = Encoding.UTF8.GetBytes(input);
                SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
                sha1.ComputeHash(clearBytes);
                hashedBytes = sha1.Hash;
                sha1.Clear();
                output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
                break;
            case HashType.SHA256:
                clearBytes = Encoding.UTF8.GetBytes(input);
                SHA256 sha256 = new SHA256Managed();
                sha256.ComputeHash(clearBytes);
                hashedBytes = sha256.Hash;
                sha256.Clear();
                output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
                break;
            case HashType.SHA384:
                clearBytes = Encoding.UTF8.GetBytes(input);
                SHA384 sha384 = new SHA384Managed();
                sha384.ComputeHash(clearBytes);
                hashedBytes = sha384.Hash;
                sha384.Clear();
                output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
                break;
            case HashType.SHA512:
                clearBytes = Encoding.UTF8.GetBytes(input);
                SHA512 sha512 = new SHA512Managed();
                sha512.ComputeHash(clearBytes);
                hashedBytes = sha512.Hash;
                sha512.Clear();
                output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
                break;
        }
        return output;
    }
}



Filed under

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

.NET Diary said:

It's very fast and work with any file size, memory usage can be easy handled.
June 5, 2006 3:47 PM

Leave a Comment

Name  

Comments 
Type the characters you see in the picture on the left.
    



Note: HTML tags in comment messages are not supported.


About Aleph

VB.NET programmer '02
C# programmer '03
Software Analyst '05
Software Architect '06