MD5 Extension Method

.NET General, Code 1 Comment

A nifty little extension method that generated the MD5/SHA-1 hash of a string. These methods are a continuation of my previous post:

public static string MD5(this string value)
{
    System.Security.Cryptography.MD5 algorithm =
        System.Security.Cryptography.MD5.Create();
    byte[] data = Encoding.ASCII.GetBytes(value);
    data = algorithm.ComputeHash(data);
    string md5 = "";
    for (int i = 0; i < data.Length; i++)
    {
        md5 += data[i].ToString("x2").ToLower();
    }
    return md5;
}

Just substitute MD5 for SHA1, SHA256, SHA384, SHA512 or RIPEMD160 which ever hash algorithm fits your needs.

One Response to “MD5 Extension Method”

  1. Veggerby : IBlog » Blog Archive » ASP.NET MVC Gravatar HtmlHelper Says:
    October 1st, 2009 at 16:13

    [...] And it also used the MD5 extension method from yesterdays post. [...]

Leave a Reply

Icons by N.Design Studio. Designed By Ben Swift. Powered by WordPress, Search Optimization and Free WordPress Themes
Entries RSS Comments RSS Log in