ASP.NET

Using MD5 Hash Algorithm

In this post on Using MD5 Hash Algorithm, I will explain the MD5 algorithm for computing hash code, the MD5 abstract class in .NET, and its implementation for computing hash code.

Basically, the Message Digest algorithm named MD5 generates a 16-byte (128-bit) hash code. This algorithm is available with the MD5 class in ASP.NET. Further, the namespace System.Security.Cryptography defines this class.

Hash Function

As a matter of fact, a hash function can compute a fixed-length small binary string known as hash code from an arbitrary text of any size. Moreover, if there is a slight change in input strings, the hash code for both is totally different. In other words, for any two input strings, it is computationally infeasible to get the same hash code.

MD5CryptoServiceProvider Class

Since the MD5 class is an abstract class, all implementations of the MD5 algorithm inherit this class. In fact, MD5CryptoServiceProvider class is inherited from the MD5 class. Also, this class is declared as sealed class and can not be inherited further.

In order to generate a hash code, this class contains a method known as ComputeHash() that returns a 16-byte hash code. The following example shows how to retrieve the hash code for an input text using this function.

Generating Hash Code Using MD5 Hash Algorithm

WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MD5Example.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            Enter some text:<br />
            <br />
            <asp:TextBox ID="TextBox1" runat="server" Columns="50" Rows="10" TextMode="MultiLine"></asp:TextBox>
            <br />
            <br />
            <hr />
            <asp:Button ID="Button2" runat="server" Text="Get Text Size" OnClick="Button2_Click" />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="Get Hash Code" OnClick="Button1_Click" />
            <br />
            <hr />
            Computed Hash Code using MD5 algorithms for above text:<br />
            <br />
            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        </div>
    </form>
</body>
</html>

WebForm1.aspx.cs

using System;
using System.Security.Cryptography;
using System.Text;
namespace MD5Example
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            String s1, s2;
            s1 = TextBox1.Text;

            s2 = GenerateHashCodeUsingMD5(s1);
            Label1.Text = s2;
        }

        public String GenerateHashCodeUsingMD5(string s)
        {
            StringBuilder str = new StringBuilder();
            MD5CryptoServiceProvider prv = new MD5CryptoServiceProvider();
            byte[] bytedata;
            bytedata=prv.ComputeHash(new UTF8Encoding().GetBytes(s));
            for(int i=0;i<bytedata.Length;i++)
            {
                str.Append(bytedata[i].ToString("x2"));
            }
            return str.ToString();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            String str = TextBox1.Text;
            int size = str.Length;
            Label2.Text = "Total Characters: " + size;
        }
    }
}

Output

Hash Code Generated Using MD5 Hash Algorithm
Hash Code Generated Using MD5 Hash Algorithm

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *