using System.Globalization;
using System.Security.Cryptography;
public static void Main()
Console.WriteLine("HashHMACBase64: " + new DigitalHMACSignature().HashHMACHex("825bb9ad1548d8f418253d42aed07415520d46f6f55c98026376387a81e36ec2", "ABCD123456ABCD123456ABCD123456ABCD123456ABCD123456"));
Console.WriteLine("ComputeHash: " + DigitalHMACSignature.ComputeHash("825bb9ad1548d8f418253d42aed07415520d46f6f55c98026376387a81e36ec2", "ABCD123456ABCD123456ABCD123456ABCD123456ABCD123456"));
public class DigitalHMACSignature
public static string ComputeHash(string key, string message)
byte[] keyBytes = new byte[key.Length / 2];
for (int i = 0; i < keyBytes.Length; i++)
keyBytes[i] = byte.Parse(key.Substring(i * 2, 2), NumberStyles.HexNumber);
byte[] encodedMessage = new ASCIIEncoding().GetBytes(message);
byte[] hash = HashHMAC(keyBytes, encodedMessage);
var computedString = BitConverter.ToString(hash).Replace("-", "").ToLower();
public string HashHMACHex(string keyHex, string message)
byte[] hash = HashHMAC(HexDecode(keyHex), StringEncode(message));
private static byte[] HashHMAC(byte[] key, byte[] message)
var hash = new HMACSHA256(key);
return hash.ComputeHash(message);
private static byte[] StringEncode(string text)
var encoding = new ASCIIEncoding();
return encoding.GetBytes(text);
private static string HashEncode(byte[] hash)
return BitConverter.ToString(hash).Replace("-", "").ToLower();
public static byte[] HexDecode(string hex)
var bytes = new byte[hex.Length / 2];
for (int i = 0; i < bytes.Length; i++)
bytes[i] = byte.Parse(hex.Substring(i * 2, 2), NumberStyles.HexNumber);
public static byte[] ConvertHexStringToByteArray(string hexString)
if (hexString.Length % 2 != 0)
throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The binary key cannot have an odd number of digits: {0}", hexString));
byte[] data = new byte[hexString.Length / 2];
for (int index = 0; index < data.Length; index++)
string byteValue = hexString.Substring(index * 2, 2);
data[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);