0% found this document useful (0 votes)
92 views4 pages

Name: Rohan Konde Roll No:52

This document describes an experiment to create and verify hash codes for messages. It provides background on hash functions, noting that they compress input of arbitrary length to a fixed length output called a hash value or message digest. The document then details features of hash functions like fixed length output and efficiency of operation. It presents a C# program that takes a sample message, generates its hash value using SHA256, and compares it to a pre-computed hash value to verify they match. The program outputs whether the hash codes match or not.

Uploaded by

Rohan Konde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views4 pages

Name: Rohan Konde Roll No:52

This document describes an experiment to create and verify hash codes for messages. It provides background on hash functions, noting that they compress input of arbitrary length to a fixed length output called a hash value or message digest. The document then details features of hash functions like fixed length output and efficiency of operation. It presents a C# program that takes a sample message, generates its hash value using SHA256, and compares it to a pre-computed hash value to verify they match. The program outputs whether the hash codes match or not.

Uploaded by

Rohan Konde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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