Monday, March 21, 2011

How to Encode Data by Using an HMAC in console application.

Obtain the key and the message to hash.
Create a byte array to hold the hash value.
Create an input stream object and an output stream object.
Create the HMAC object to perform hashing.
Call the ComputeHash method. Assign the results to the array.
Write the hash array into the output stream.
Send the hash value and the original message to the recipient.
You can use HMACs to encode files or message data. The .NET Framework includes HMAC implementations that are constructed from hash algorithms. For example, the HMACSHA1 class is a keyed hash algorithm that is constructed from the SHA1 hash function. You use the same pattern to encode and decode HMACs across all of these classes.
The HMAC process mixes a secret key with the message data, hashes the result with the hash function, mixes that hash value with the secret key again, and then applies the hash function a second time. The output hash is 160 bits in length.

Example of HMAC: create a windows application insert two button, label and textbox:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace module4
public partial class hmachash : Form
String x_str;
byte[] x_message_data;
string x_key_string;
byte[] x_key_bytes;
byte[] x_hash_code;

public hmachash()

private void button1_Click(object sender, EventArgs e)
// define the string that we will
// create a hash code for
x_str = textBox1.Text;
// create a byte array from the string
x_message_data = Encoding.Default.GetBytes(x_str);

// define the secret key as a string
x_key_string = "This is my secret key";
// create a byte array containing the key
x_key_bytes = Encoding.Default.GetBytes(x_key_string);

// create an instance of the HMAC-SHA-1 keyed algorithm
KeyedHashAlgorithm x_hash_alg = KeyedHashAlgorithm.Create("HMACSHA1");

// set the secret key for the hashing algorithm
x_hash_alg.Key = x_key_bytes;

// obtain the hash code from the HashAlgorithm by
// using the ComputeHash method
x_hash_code = x_hash_alg.ComputeHash(x_message_data);
// MessageBox.Show(x_hash_code.Length.ToString());
// print out the hash code to the console
foreach (byte x_byte in x_hash_code)
label1.Text+=" " + x_byte;

private void button2_Click(object sender, EventArgs e)
label1.Text = "";

x_str = textBox1.Text;
// create a byte array from the string
x_message_data = Encoding.Default.GetBytes(x_str);

//1 create algo and pass the secret key
HMACSHA1 hm = new HMACSHA1(x_key_bytes);
//2 generate hash code again to verify
byte[] computehash = hm.ComputeHash(x_message_data);

bool same = true;
for (int i = 0; i <>
if (computehash[i] != x_hash_code[i])
// label1.Text +=" invalid hash ";
same = false;
//label1.Text += "valid hash";
same = true;

label1.Text = same.ToString();

private void hmachash_Load(object sender, EventArgs e)

Output is display below:


No comments:

Recent Posts