Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyedHashAlgorithm in .net core

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?

like image 706
Ross Avatar asked Oct 16 '25 03:10

Ross


1 Answers

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));
}
like image 95
tpeczek Avatar answered Oct 17 '25 16:10

tpeczek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!