Name: Rohan Konde Roll No:52

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Name: Rohan Konde

Roll No:52

Experiment No. 7

Topic: - Create and Verify hash code for given message

Cryptography Hash functions


Hash functions are extremely useful and appear in almost all information security applications.

A hash function is a mathematical function that converts a numerical input value into another
compressed numerical value. The input to the hash function is of arbitrary length but output is
always of fixed length.

Values returned by a hash function are called message digest or simply hash values. The
following picture illustrated hash function –

Features of Hash Functions

The typical features of hash functions are −

1).Fixed Length Output (Hash Value)

 Hash function coverts data of arbitrary length to a fixed length. This process is
often referred to as hashing the data.

 In general, the hash is much smaller than the input data, hence hash functions are
sometimes called compression functions.
 Since a hash is a smaller representation of a larger data, it is also referred to as a digest.
 Hash function with n bit output is referred to as an n-bit hash function. Popular
hash functions generate values between 160 and 512 bits.

2). Efficiency of Operation

 Generally for any hash function h with input x, computation of h(x) is a fast operation.

 Computationally hash functions are much faster than a symmetric encryption.

Program Verifying hash code for given message: -

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

class Class1
{
static void Main()
{
//This hash value is produced from "This is the original message!"
//using SHA256.
byte[] sentHashValue = { 185, 203, 236, 22, 3, 228, 27, 130, 87, 23, 244, 15,
87, 88, 14, 43, 37, 61, 106, 224, 81, 172, 224, 211, 104, 85, 194, 197, 194, 25, 12 0,
217 };

//This is the string that corresponds to the previous hash value.


string messageString = "This is the original message!";

byte[] compareHashValue;

//Create a new instance of the UnicodeEncoding class to


//convert the string into an array of Unicode bytes.
UnicodeEncoding ue = new UnicodeEncoding();

//Convert the string into an array of bytes.


byte[] messageBytes = ue.GetBytes(messageString);

//Create a new instance of the SHA256 class to create


//the hash value.
SHA256 shHash = SHA256.Create();

//Create the hash value from the array of bytes.


compareHashValue = shHash.ComputeHash(messageBytes);

bool same = true;


//Compare the values of the two byte
arrays. for (int x = 0; x <
sentHashValue.Length; x++)
{
if (sentHashValue[x] != compareHashValue[x])
{
same = false;
}
}
//Display whether or not the hash values are
the same. if (same)
{
Console.WriteLine("The hash codes match.");
}
else
{
Console.WriteLine("The hash codes do not match.");
}
}

If the two hash values match, this code displays the following to the

console: Console

Copy
The hash codes match.
If they do not match, the code displays the

following: Console

Copy
The hash codes do not match.

You might also like