Search code examples
rsasigningrsacryptoserviceprovider

Why is SHA256 and invalid hash algorithm when signing with RSA?


I am trying to sign a stream using the method below but I always get a Cryptographic Exception saying that it is not a valid hash algorithm even though it is listed in the documentation:

byte[] SignData(RSACryptoServiceProvider rsa, Stream stream, string hashName)
{
    HashAlgorithm hash = HashAlgorithm.Create(hashName);
    byte[] signature = rsa.SignData(stream, hash);
    return signature;
}

I am calling it with "SHA256" as the hashName parameter. and the exception is thrown at the line where rsa.SignData() is called.

This is NOT using X509 certificates, it is not a duplicate. BTW see answer below.


Solution

  • Just resolved and it is not a duplicate because I am using the plain RSACryptoServiceProvider of .NET and not X509 certificates.

    I ran into this post: http://hintdesk.com/c-how-to-fix-invalid-algorithm-specified-when-signing-with-sha256/

    Reading it it occurred to me to to examine the RSA SignatureAlgorithm and it the resulting URI indicated it was SHA1 so no SHA256 or SHA512. Then I looked up that property in MSDN documentation and it says that property always returns that value because it only supports that, so only SHA1.