Computing hash codes in C# with code snippet

Category > CSHARP || Published on : Wednesday, October 16, 2019 || Views: 5311 || computing hash codes in C# hash codes in C#


Introduction

Here Pawan Kumar will explain how to All in all, computing hash codes in C# is useful across a number of scenarios.

Description

In previous post I have explained Display TextBox when CheckBox is checked in ASP.Net using C#, Get multiple selected (checked) CheckBox values as Comma Separated String using jQuery, How to display values only upto 2 decimal places, Simple way to convert datarow array to datatable, Sending Message Telegram through ASP.NET C#, Open new tab on LinkButton Click using C# and VB.Net in ASP.Net, and many more articles.

Now I will explain How to All in all, computing hash codes in C# is useful across a number of scenarios.

So follow the steps to learn All in all, computing hash codes in C# is useful across a number of scenarios.

Note:- You have to include below two namespace in webform

  • using System.Security.Cryptography;
  • using System.Text;

Step 1 - Create a asp.net website using Visual Studio 2019 or any other version of visual studio as you have installed on your system.


Step 2 - Add a new Webform C# into the website created in step 1


Step 3 - Drag and drop 2 lables, 1 textbox and 1 button on the webform.


Step 4 -  cs.aspx codes are below for Computing hash values in C#

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cs.aspx.cs" Inherits="examples_2_cs" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h2>Computing hash values in C# - MD5 with source codes</h2>
            <asp:Label ID="Label1" runat="server" Text="Enter text to convert"></asp:Label>
            <asp:TextBox ID="tbPlainText" runat="server"></asp:TextBox>
            <br />
            <asp:Label ID="lblChangedText" runat="server" Text=""></asp:Label>
            <br />
            <asp:Button ID="btnSubmit" runat="server" Text="Convert to MD5" OnClick="btnSubmit_Click" />
        </div>
    </form>
</body>
</html>

Step 5: Write the below codes in code behind file of cs.aspx.cs


using System;
using System.Security.Cryptography;
using System.Text;

public partial class examples_2_cs :  System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private string MD5(string input)
    {
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

        byte[] originalBytes = ASCIIEncoding.Default.GetBytes(input);
        byte[] encodedBytes = md5.ComputeHash(originalBytes);

        return BitConverter.ToString(encodedBytes).Replace("-", "");
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
       lblChangedText.Text=  MD5(tbPlainText.Text.Trim());
    }
}

Step 6: Now run the project by pressing F5. All in all, computing hash codes in C# is useful across a number of scenarios.

Conclusion:

So, In this tutorial we have learned, All in all, computing hash codes in C# is useful across a number of scenarios.