Oleksii
Oleksii

Reputation: 1549

RSA.SignHash SHA512 and SHA256 give the same signature length

Strange, but signature array length are the same for the code:

using (RSA rsa = certifiateToUse.GetRSAPrivateKey())
{
    byte[] bytesData = Encoding.UTF8.GetBytes(input);
    byte[] hash512 = rsa.SignHash(bytesData, HashAlgorithmName.SHA512, RSASignaturePadding.Pss);
    byte[] hash256 = rsa.SignHash(bytesData, HashAlgorithmName.SHA256, RSASignaturePadding.Pss);
}

Both hash512 and hash256 have 512 array length. I can not find a reasonable answer to this. Maybe I'm missing something obvious? I expected it to be 512 and 256 bytes long each.

Upvotes: 4

Views: 5213

Answers (1)

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137438

SHA-256 and SHA-512 produce 256 and 512 bit hashes, respectively.

Your RSA key is 1024 or 2048 bits, and the block size when using it as a cipher is the same.

Since both hashes are smaller than the RSA block size, they need to be padded out to that size. And because they are padded, the resulting signatures will be the same size regardless of the hash algorithm used.

The clue here is the last parameter, which specifies the padding algorithm.

Upvotes: 9

Related Questions