Standard: KeyedHashAlgorithm.Create("HMACSHA1") throw "Operation is not supported on this platform " Exception

Created on 9 Oct 2017  Â·  8Comments  Â·  Source: dotnet/standard

.net standard 2.0 , The following code throw Operation is not supported on this platform exception。

using (var algorithm = KeyedHashAlgorithm.Create("HMACSHA1"))
{
//... some codes
}

Most helpful comment

Apparently not. Reactivated the issue which discussed how we missed some.

All 8 comments

protected override string ComputeSignatureCore(string key, string data)
{
//using (var algorithm = KeyedHashAlgorithm.Create(SignatureMethod.ToUpperInvariant()))
//{
// algorithm.Key = Encoding.GetBytes(key.ToCharArray());
// return Convert.ToBase64String(
// algorithm.ComputeHash(Encoding.GetBytes(data.ToCharArray())));
//}
using (var hashAlgorithm = new HMACSHA1(Encoding.GetBytes(key.ToCharArray())))
{
return Convert.ToBase64String(
hashAlgorithm.ComputeHash(Encoding.GetBytes(data.ToCharArray())));
}
}

this code is ok!

Fence code like this for block formatting, and add cs for syntax highlighting:

````markdown

// Some C#

````

I hit this error as well - .NET Core 2.0 (targeting .NET Standard 2.0) with HmacSHA256 algorithm type. I'm not even sure how to get a list of supported algorithms.

From the immediate window:

> System.Security.Cryptography.KeyedHashAlgorithm.Create("HmacSHA256")
'System.Security.Cryptography.KeyedHashAlgorithm.Create("HmacSHA256")' threw an exception of type 'System.PlatformNotSupportedException'
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146233031
    HelpLink: null
    InnerException: null
    Message: "Operation is not supported on this platform."
    Source: "System.Security.Cryptography.Primitives"
    StackTrace: "   at System.Security.Cryptography.KeyedHashAlgorithm.Create(String algName)"
    TargetSite: {System.Security.Cryptography.KeyedHashAlgorithm Create(System.String)}

However this works in LINQPad, so it must be something that was dropped in .NET Core

As mentioned above, new HMACSHAC256() works fine in its place, but it's a sneaky incompatibility nonetheless.

If your identifier is a literal, you should just change to using the class directly.

```C#
using (KeyedHashAlgorithm alg = KeyedHashAlgorithm.Create("HmacSHA256"))
{
alg.Key = key;
...
}

```C#
using (KeyedHashAlgorithm alg = new HMACSHA256(key))
{
    ...
}

If you are dynamic and trying to maintain the dynamism:

```C#
using (KeyedHashAlgorithm alg = CryptoConfig.CreateFromName(algId) as KeyedHashAlgorithm))
{
if (alg == null)
{
throw new IDontHaveThatAlgorithmException();
}

alg.Key = key;
...

}
```

This code change is minor, but I'm really more concerned that .NET Core appears to adhere to .NET Standard here but then just fails at runtime.

This creates the illusion that your code is compatible with .NET Core (it compiles fine) but it doesn't actually work. Why not just leave the API out if it's not really supported?

Essentially, what happened is that .NET Core didn't have the methods, then in .NET Standard 2.0 all members from the .NET Framework surface area were added to .NET Standard (on types that were already in .NET Standard), and .NET Core gained them as throw (because CryptoConfig was still not present). Then CryptoConfig came back at the last minute and we missed wiring these methods back up in Core.

Hopefully the number of places that fell into the corners like this is small.

Then CryptoConfig came back at the last minute and we missed wiring these methods back up in Core.

Have they all been fixed for 2.1?

Apparently not. Reactivated the issue which discussed how we missed some.

Was this page helpful?
0 / 5 - 0 ratings