I need to convert the following .Net code to .Net Core:
static byte[] HmacSHA256(String data, byte[] key)
{
String algorithm = "HmacSHA256";
KeyedHashAlgorithm kha = KeyedHashAlgorithm.Create(algorithm);
kha.Key = key;
return kha.ComputeHash(Encoding.UTF8.GetBytes(data));
}
The above snippet is used for Amazon AWS key signing and is taken from here.
I'm using System.Security.Cryptography.Primitives 4.3.0 and KeyedHashAlgorithm.Create method doesn't exists. Looking at the github I can see that the Create method is there now, but it's not supported:
public static new KeyedHashAlgorithm Create(string algName)
{
throw new PlatformNotSupportedException();
}
The question is what is my alternative to KeyedHashAlgorithm.Create(string algName) in .Net Core?
The .Net Core seems to be providing HMACSHA256 Class which should be exactly what you need:
static byte[] HmacSHA256(String data, byte[] key)
{
HMACSHA256 hashAlgorithm = new HMACSHA256(key);
return hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(data));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With