Net Encryption Library
HMAC Examples
Basic Tasks > HMAC Examples

HMAC Overview

The .NET Encryption library supports HMACSHA256, HMACSHA384, HMACSHA512 and HMACMD596. 

https://en.wikipedia.org/wiki/HMAC

 

/// <summary>

/// See the first example from http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/HMAC_SHA256.pdf

/// </summary>

[Test]

public void TestHMAC_SHA2_256()

{

Encryption encryption = new Encryption(); //Trial Mode

//Encryption encryption = new Encryption("place user name here", "place license key here"); //License Mode

byte[] input = encryption.HexadecimalStringToByteArray("53616D706C65206D65737361676520666F72206B65796C656E3D626C6F636B6C656E");

byte[] key = encryption.HexadecimalStringToByteArray("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F");

string hashString = encryption.HashBytesHex(HashProvider.HMACSHA256, input, key);

Assert.AreEqual("8BB9A1DB9806F20DF7F77B82138C7914D174D59E13DC4D0169C9057B133E1D62", hashString);

}

 

/// <summary>

/// See the second example from http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/HMAC_SHA256.pdf

/// </summary>

[Test]

public void TestHMAC_SHA2_256_96()

{

Encryption encryption = new Encryption(); //Trial Mode

//Encryption encryption = new Encryption("place user name here", "place license key here"); //License Mode

byte[] input = encryption.HexadecimalStringToByteArray("53616D706C65206D65737361676520666F72206B65796C656E3C626C6F636B6C656E");

byte[] key = encryption.HexadecimalStringToByteArray("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");

string hashString = encryption.HashBytesHex(HashProvider.HMACSHA25696, input, key);

Assert.AreEqual("A28CF43130EE696A98F14A37", hashString);

}

 

/// <summary>

/// See the first example from http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/HMAC_SHA384.pdf

/// </summary>

[Test]

public void TestHMAC_SHA2_384()

{

Encryption encryption = new Encryption(); //Trial Mode

//Encryption encryption = new Encryption("place user name here", "place license key here"); //License Mode

byte[] input = encryption.HexadecimalStringToByteArray("53616D706C65206D65737361676520666F72206B65796C656E3D626C6F636B6C656E");

byte[] key = encryption.HexadecimalStringToByteArray("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F");

string hashString = encryption.HashBytesHex(HashProvider.HMACSHA384, input, key);

Assert.AreEqual("63C5DAA5E651847CA897C95814AB830BEDEDC7D25E83EEF9195CD45857A37F448947858F5AF50CC2B1B730DDF29671A9", hashString);

}

 

/// <summary>

/// See the first example from http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/HMAC_SHA512.pdf

/// </summary>

[Test]

public void TestHMAC_SHA2_512()

{

Encryption encryption = new Encryption(); //Trial Mode

//Encryption encryption = new Encryption("place user name here", "place license key here"); //License Mode

byte[] input = encryption.HexadecimalStringToByteArray("53616D706C65206D65737361676520666F72206B65796C656E3D626C6F636B6C656E");

byte[] key = encryption.HexadecimalStringToByteArray("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F");

string hashString = encryption.HashBytesHex(HashProvider.HMACSHA512, input, key);

Assert.AreEqual("FC25E240658CA785B7A811A8D3F7B4CA48CFA26A8A366BF2CD1F836B05FCB024BD36853081811D6CEA4216EBAD79DA1CFCB95EA4586B8A0CE356596A55FB1347",

hashString);

}

 

/// <summary>

/// See the second example from http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/HMAC_SHA512.pdf

/// </summary>

[Test]

public void TestHMAC_SHA2_512_96()

{

Encryption encryption = new Encryption(); //Trial Mode

//Encryption encryption = new Encryption("place user name here", "place license key here"); //License Mode

byte[] input = encryption.HexadecimalStringToByteArray("53616D706C65206D65737361676520666F72206B65796C656E3C626C6F636B6C656E");

byte[] key = encryption.HexadecimalStringToByteArray("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F");

string hashString = encryption.HashBytesHex(HashProvider.HMACSHA51296, input, key);

Assert.AreEqual("FD44C18BDA0BB0A6CE0E82B0", hashString);

}

 

/// <summary>

/// See example A.1 in APPENDIX A from http://csrc.nist.gov/publications/fips/fips198/fips-198a.pdf

/// </summary>

[Test]

public void TestHMAC_SHA1()

{

Encryption encryption = new Encryption(); //Trial Mode

//Encryption encryption = new Encryption("place user name here", "place license key here"); //License Mode

byte[] input = Encoding.ASCII.GetBytes("Sample #1");

byte[] key = encryption.HexadecimalStringToByteArray("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f");

string hashString = encryption.HashBytesHex(HashProvider.HMACSHA1, input, key);

Assert.AreEqual("4F4CA3D5D68BA7CC0A1208C9C61E9C5DA0403C0A", hashString);

}

 

/// <summary>

/// See the first example from http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/HMAC_SHA1.pdf

/// </summary>

[Test]

public void TestHMAC_SHA1_96()

{

Encryption encryption = new Encryption(); //Trial Mode

//Encryption encryption = new Encryption("place user name here", "place license key here"); //License Mode

byte[] input = encryption.HexadecimalStringToByteArray("53616D706C65206D65737361676520666F72206B65796C656E3D626C6F636B6C656E");

byte[] key = encryption.HexadecimalStringToByteArray("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F");

string hashString = encryption.HashBytesHex(HashProvider.HMACSHA196, input, key);

Assert.AreEqual("5FD596EE78D5553C8FF4E72D", hashString);

}