Tip

Hashing strings with any algorithm

This tip was submitted to the SearchVB.com tip exchange by member Stefan Prodan. Please let others know how useful it is via the rating scale at the end of the tip. Do you have a useful VB, .NET or Visual Studio tip or code to share? Submit it here.


When dealing with messages in an application, if the message you are receiving or sending is over a not trusted channel, then you should think about a way to make sure that the message has not been altered. This is why cryptographic hash functions are used.

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;
    }
}

This was first published in October 2006

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.