Skip to content

Instantly share code, notes, and snippets.

@PaulNichols
Created January 9, 2017 04:54
Show Gist options
  • Save PaulNichols/4b2b4280f509822cd8d792efbf1b2e9c to your computer and use it in GitHub Desktop.
Save PaulNichols/4b2b4280f509822cd8d792efbf1b2e9c to your computer and use it in GitHub Desktop.
Cryptography - hash message authentication code (HMAC)
using System.Security.Cryptography;
namespace CryptographyInDotNet
{
public class Hmac
{
private const int KeySize = 32;
public static byte[] GenerateKey()
{
using (var randomNumberGenerator = new RNGCryptoServiceProvider())
{
var randomNumber = new byte[KeySize];
randomNumberGenerator.GetBytes(randomNumber);
return randomNumber;
}
}
public static byte[] ComputeHmacsha256(byte[] toBeHashed, byte[] key)
{
using (var hmac = new HMACSHA256(key))
{
return hmac.ComputeHash(toBeHashed);
}
}
public static byte[] ComputeHmacsha1(byte[] toBeHashed, byte[] key)
{
using (var hmac = new HMACSHA1(key))
{
return hmac.ComputeHash(toBeHashed);
}
}
public static byte[] ComputeHmacsha512(byte[] toBeHashed, byte[] key)
{
using (var hmac = new HMACSHA512(key))
{
return hmac.ComputeHash(toBeHashed);
}
}
public static byte[] ComputeHmacmd5(byte[] toBeHashed, byte[] key)
{
using (var hmac = new HMACMD5(key))
{
return hmac.ComputeHash(toBeHashed);
}
}
}
}
using System;
using System.Text;
namespace CryptographyInDotNet
{
class Program
{
static void Main()
{
const string originalMessage = "Original Message to hash";
const string originalMessage2 = "Original xessage to hash";
Console.WriteLine("HMAC Demonstration in .NET");
Console.WriteLine("--------------------------");
Console.WriteLine();
var key = Hmac.GenerateKey();
var hmacMd5Message = Hmac.ComputeHmacmd5(Encoding.UTF8.GetBytes(originalMessage), key);
var hmacMd5Message2 = Hmac.ComputeHmacmd5(Encoding.UTF8.GetBytes(originalMessage2), key);
var hmacSha1Message = Hmac.ComputeHmacsha1(Encoding.UTF8.GetBytes(originalMessage), key);
var hmacSha1Message2 = Hmac.ComputeHmacsha1(Encoding.UTF8.GetBytes(originalMessage2), key);
var hmacSha256Message = Hmac.ComputeHmacsha256(Encoding.UTF8.GetBytes(originalMessage), key);
var hmacSha256Message2 = Hmac.ComputeHmacsha256(Encoding.UTF8.GetBytes(originalMessage2), key);
var hmacSha512Message = Hmac.ComputeHmacsha512(Encoding.UTF8.GetBytes(originalMessage), key);
var hmacSha512Message2 = Hmac.ComputeHmacsha512(Encoding.UTF8.GetBytes(originalMessage2), key);
Console.WriteLine();
Console.WriteLine("MD5 HMAC");
Console.WriteLine();
Console.WriteLine("Message 1 hash = " + Convert.ToBase64String(hmacMd5Message));
Console.WriteLine("Message 2 hash = " + Convert.ToBase64String(hmacMd5Message2));
Console.WriteLine();
Console.WriteLine("SHA 1 HMAC");
Console.WriteLine();
Console.WriteLine("Message 1 hash = " + Convert.ToBase64String(hmacSha1Message));
Console.WriteLine("Message 2 hash = " + Convert.ToBase64String(hmacSha1Message2));
Console.WriteLine();
Console.WriteLine("SHA 256 HMAC");
Console.WriteLine();
Console.WriteLine("Message 1 hash = " + Convert.ToBase64String(hmacSha256Message));
Console.WriteLine("Message 2 hash = " + Convert.ToBase64String(hmacSha256Message2));
Console.WriteLine();
Console.WriteLine("SHA 512 HMAC");
Console.WriteLine();
Console.WriteLine("Message 1 hash = " + Convert.ToBase64String(hmacSha512Message));
Console.WriteLine("Message 2 hash = " + Convert.ToBase64String(hmacSha512Message2));
Console.WriteLine();
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment