Tuesday, March 15, 2011

Understanding the Hashing Data in console application.

A hash value is a numeric value of a fixed length that uniquely identifies data. You can use hash values with digital signatures because hash values represent a large amount of data as a much smaller numeric value. You can efficiently sign a hash value instead of signing the larger value. You can also use hash values to verify the integrity of data that is sent through insecure channels. You can generate a hash value of data that you have received and compare it to the hash value of the data that was sent to determine whether the data was altered in transit.

How to Generate a Hash Value:
To generate a hash value:
Create a byte array to hold the computed hash.
Obtain the data that you want to hash.
Convert the data to a byte array or a stream.
Create a Hash object to generate the hash.
Call the ComputeHash method.
A hash value is a numeric value of a fixed length that uniquely identifies data. You can use one of several .NET Framework 2.0 classes to create and verify hash values.

 The .NET Framework provides several classes that you can use to perform this operation. These include the SHA1Managed class, the SHA1CryptoServiceProvider class, and the MD5CryptoServiceProvider class.
 You can use the following approach with any class that performs hash operations:
1. Create a byte array variable to hold the generated hash.
2. Obtain the data that you want to hash.
3. Convert the data to a byte array or a stream that the ComputeHash method can process.
4. Create an object instance of the class that will generate the hash. For example, you can create an MD5 object, a SHA1 object, or one of the objects that implement these algorithms.
5. Call the ComputeHash method on the object that you created in Step 4, and assign
its result to the byte array that you created in Step 1.

Example of Hash value:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
class Class1
{
    static void Main(string[] args)
{
byte[] HashValue;
string MessageString = "This is the original message!";
//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 SHA1Managed class to create
//the hash value.
SHA1Managed SHhash = new SHA1Managed();
//Create the hash value from the array of bytes.
HashValue = SHhash.ComputeHash(MessageBytes);
//Display the hash value to the console.
foreach(byte b in HashValue)
{
Console.Write("{0} ", b);
}
Console.ReadLine();
}
}
Note: They use the SHA1Managed class to hash a string of text. The code first creates the byte array to hold the hash, defines the original message, and then creates a UnicodeEncoding object to convert the original message into a byte array. Next, the code creates a SHA1Managed object and calls the ComputeHash method to generate the hash value and assign it to the byte array that was created to hold the hash.

Output is display below:

No comments:

Recent Posts