C# Create a MD5 hash representation of a string

By [)ia6l0 iii

Use the System.Security.Cryptography.MD5 class to create a MD5 representation of your string

Below is a sample snippet:

public string ComputeMD5Hash(string myString)
{
    MD5 md5 = System.Security.Cryptography.MD5.Create();
    byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(myString);
    byte[] hash = md5.ComputeHash(inputBytes);

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
         sb.Append(hash[i].ToString("x2"));
    }
    return sb.ToString();
}

Related FAQs

Use the Environment class to get important and user-specific properties like the User Profile folder.
Named and optional parameters is a new concept introduced in 4.0. The Named Parameters allow you to pass arguments by parameter name instead of parameter position in the signature. Optional Parameters allow parameters to be specified as optional by providing a default value for them in a member declaration. When the member is invoked, optional arguments can be omitted.
Extension method to get the start of a day
Extension method to get the elasped time
Use the following code snippet to download an image from an URL.
You can create short url's using the Google URL Shortening API
C# Create a MD5 hash representation of a string  (1798 Views)