Search code examples
c#mono.net-3.5cryptographysha1

Mono doesn't recognize SHA1 as hash algorithm for RSA signing


I am trying to port some code from C# to Mono for a future Linux project.

Mono outputs this error:

A System.Security.Cryptography.CryptographyException was thrown; sha1 is an unsupported hash algorithm for RSA signing"

when running or debugging this code for RSA signing:

byte[] bytesSing = rsa.SignHash(hashValue, "SHA1");

I checked with VS2010 and the reference says it must be a string, which I passed as "SHA1". This compiled and ran under Windows, but does not compile or run under Mono.

Here's Mono's source code.


Solution

  • Based on the source code file you linked, the SignHash function assumes that the string you're passing is an OID, and tries to look it up as such in the GetHashNameFromOID function directly above it.

    The Mono documentation supports this, documenting that parameter as:

    The hash algorithm identifier (OID) used to create the hash value of the data.

    The Microsoft implementation looks the parameter up through X509Utils.NameOrOidToAlg, which probably accepts either a name or an OID.

    So to get it working, you should be able to pass null in the Mono version, or pass the OID "1.3.14.3.2.26".