Programmingempire
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" />
<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

Further Reading
Parameter and ParameterCollection in ADO.NET
Database Manipulation Using DataGrid
Example of Button and Link Button Control in ASP.NET
Example of Chart Control in ASP.NET
Creating a DataTable from a DataReader in ASP.NET
Deleting a Record using DataGrid Control in ASP.NET
Edit a Record Using DataGrid Control in ASP.NET
Insert a Record Using ItemCommand Event in DataGrid
CRUD Operations with DataGrid in ASP.NET
Creating Columns in a DataGrid Control
XML Documents and DataSet in ASP.NET
ASP.NET Core Features and Advantages
Display Images Using DataList Control
Adding Images Using Image Control
Creating a Group of Radio Buttons Using RadioButtonList Control
Example of Button Control in ASP.NET
ItemDataBound Event in DataList
More Features of DataList in ASP.NET
A Simple Example of Using a DataList Control in ASP.NET
Properties and Methods of DataList Control in ASP.NET
Exploring DataList Control in ASP.NET
Custom Validator Control in ASP.NET
Validation Summary Control in ASP.NET
Validation Controls Examples – RequiredFieldValidator, CompareValidator, and RangeValidator
An Example of Data Binding with RadioButtonList Control
Binding Data to Web Control in ADO.NET
Examples of AdRotator Control in ASP.NET
Examples of Validation Controls in ASP.NET
Overview of MVC architecture in ASP.NET
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
