Skip to content

Instantly share code, notes, and snippets.

@dhcgn
Created September 22, 2016 20:19
Show Gist options
  • Save dhcgn/ed15f69692821c2e3a4ebbc05f9b6b33 to your computer and use it in GitHub Desktop.
Save dhcgn/ed15f69692821c2e3a4ebbc05f9b6b33 to your computer and use it in GitHub Desktop.
void Main()
{
{ // 0. Create a HMAC
byte[] keyHmac = new byte[] { 255 };
var hmac = new HMACSHA512(keyHmac); // 1st HMACSHA512 ctor
var result = hmac.ComputeHash(new byte[] { 1, 2, 3 });
Console.Out.WriteLine($"0. - {Convert.ToBase64String(result)}");
}
Console.Out.WriteLine("\r\nThe following MACs should be all equal!\r\n");
{ // 1. Create a HMAC than add a byte to the result and create a second HMAC
byte[] keyHmac = new byte[] { 255 };
var hmac = new HMACSHA512(keyHmac); // 1st HMACSHA512 ctor
var result = hmac.ComputeHash(new byte[] { 1, 2, 3 });
result = hmac.ComputeHash(result.Concat(new byte[] { 7 }).ToArray());
Console.Out.WriteLine($"1. - {Convert.ToBase64String(result)}");
}
{ // 2. Create a HMAC than add a byte to the result and create a second HMAC with a new instance
byte[] keyHmac = new byte[] { 255 };
var hmac = new HMACSHA512(keyHmac); // 1st HMACSHA512 ctor
var result = hmac.ComputeHash(new byte[] { 1, 2, 3 });
hmac = new HMACSHA512(keyHmac); // 2nd HMACSHA512 ctor
result = hmac.ComputeHash(result.Concat(new byte[] { 7 }).ToArray());
Console.Out.WriteLine($"2. - {Convert.ToBase64String(result)}");
}
{ // 3. Create a HMAC from a stream than add a byte to the result and create a second HMAC
byte[] keyHmac = new byte[] { 255 };
var hmac = new HMACSHA512(keyHmac);
using (var resultStream = new MemoryStream())
{
using (var hmacStream = new CryptoStream(resultStream, hmac, CryptoStreamMode.Write))
{
new MemoryStream(new byte[] { 1, 2, 3 }).CopyTo(hmacStream);
}
}
var result = hmac.Hash;
result = hmac.ComputeHash(result.Concat(new byte[] { 7 }).ToArray());
Console.Out.WriteLine($"3. - {Convert.ToBase64String(result)}");
}
{ // 4. Create a HMAC from a stream than add a byte to the result and create a second HMAC with a new instance
byte[] keyHmac = new byte[] { 255 };
var hmac = new HMACSHA512(keyHmac);
using (var resultStream = new MemoryStream())
{
using (var hmacStream = new CryptoStream(resultStream, hmac, CryptoStreamMode.Write))
{
new MemoryStream(new byte[] { 1, 2, 3 }).CopyTo(hmacStream);
}
}
var result = hmac.Hash;
hmac = new HMACSHA512(keyHmac);
result = hmac.ComputeHash(result.Concat(new byte[] { 7 }).ToArray());
Console.Out.WriteLine($"4. - {Convert.ToBase64String(result)}");
}
}
// 0. - J0x6KRHzGh1nTLL+a+pL8H9PJyl1b9/rL7D0j3S1DBpMduct37uMi0mBFEOdkfrLs2Ipn39yoV6GaRoEK+hU7A==
//
// The following MACs should be all equal!
//
// 1. - mVd7YQ7AbmRfH57AprAuU1vlSuOucvg+NbUFl7eNurPuvGS/Xrko2Kz3d9vUGXr0P287dOgEKQJDNfMkN2xi5Q==
// 2. - mVd7YQ7AbmRfH57AprAuU1vlSuOucvg+NbUFl7eNurPuvGS/Xrko2Kz3d9vUGXr0P287dOgEKQJDNfMkN2xi5Q==
// 3. - DVncxk/dEYhmmpK5qEnVg0Pc0/MUe8APbAiyZrh+ba35oGv2TGCkFco3gFVZ2gl+h3DpcqP7VbmuthBmCvSKlg==
// 4. - mVd7YQ7AbmRfH57AprAuU1vlSuOucvg+NbUFl7eNurPuvGS/Xrko2Kz3d9vUGXr0P287dOgEKQJDNfMkN2xi5Q==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment