public static ComputeKeyedHash ( this data, string key, |
||
data | this | byte[] of content to hash |
key | string | secret key to salt the hash. This is assumed to be UTF-8 encoded |
hashType | determines which alogirthm to use. The recommendation is to use HMAC-SHA256 | |
return | byte[] |
public static byte[] ComputeKeyedHash(this byte[] data, string key, KeyedHashAlgorithm hashType = KeyedHashAlgorithm.HMACSHA256)
{
if (data == null) return null;
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException("key");
using (var algorithm = HMAC.Create(hashType.ToString()))
{
algorithm.Key = key.ToByteArray();
return algorithm.ComputeHash(data);
}
}
public void ComputeKeyedHashNullException() { ExtensionMethods.ComputeKeyedHash("value".ToByteArray(), null); }