ASP.NET/VB: MD5 Hash a String

Using ASP.NET/VB, I want to be able to use MD5 to hash a string like this:
response.write(md5("Hello World"))

Open in new window

LVL 16
hankknightAsked:
Who is Participating?
 
ravikantninaveConnect With a Mentor Commented:
string hash = CalculateMD5Hash("abcdefghijklmnopqrstuvwxyz");
returns a string like this:

C3FCD3D76192E4007DFB496CCA67E13B

public string CalculateMD5Hash(string input)
{
    // step 1, calculate MD5 hash from input
    MD5 md5 = System.Security.Cryptography.MD5.Create();
    byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
    byte[] hash = md5.ComputeHash(inputBytes);
 
    // step 2, convert byte array to hex string
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
        sb.Append(hash[i].ToString("X2"));
    }
    return sb.ToString();
}

Open in new window

0
 
ravikantninaveCommented:
To get output in lowercase change in CalculteMD5Hash:

sb.Append(hash[i].ToString("x2"));

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.