public string HMACSign(byte[] data, string key, SigningAlgorithm algorithmName)
{
if (String.IsNullOrEmpty(key))
throw new ArgumentNullException("key", "Please specify a Secret Signing Key.");
if (data == null || data.Length == 0)
throw new ArgumentNullException("data", "Please specify data to sign.");
KeyedHashAlgorithm algorithm = ThreadSafeCreateKeyedHashedAlgorithm(algorithmName);
if (null == algorithm)
throw new InvalidOperationException("Please specify a KeyedHashAlgorithm to use.");
try
{
algorithm.Key = Encoding.UTF8.GetBytes(key);
byte[] bytes = algorithm.ComputeHash(data);
return Convert.ToBase64String(bytes);
}
finally
{
algorithm.Clear();
}
}