• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 852
  • Last Modified:

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

0
hankknight
Asked:
hankknight
  • 2
2 Solutions
 
ravikantninaveCommented:
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now