We have a signing service that accepts a sha256 hash as an input and signs the hash using pkcs11 and bouncy castle libraries in C# encode the signed digest to Bae64 and sends it to the requester. So, essentially we are generating a hash of a hash and signing the hash.
The requester on the other end in order to verify this decodes the base64 received digest and verifies it. I have used a .NET library at my end in PowerShell and implemented a verification process that verifies. please see below.
## Load a certificate Public key and verify
$certusedforsigning = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2
$certusedforsigning.Import($signingcertificate)
$certusedforsigning.PublicKey.key -as [System.Security.Cryptography.RSACryptoServiceProvider] | Out-null
$oid = [System.Security.Cryptography.CryptoConfig]::MapNameToOID('SHA256')
$padding = [System.Security.Cryptography.RSASignaturePadding]::Pkcs1
$result = $certusedforsigning.PublicKey.key.Verifyhash($originalhashinBinary,$signedbinary,'SHA256',$padding)
$result
if($result -eq 'True' ) { write-host 'Signature verified True'} else { write-host 'Signature validation Failed'}
}
}
Now, in order to get around hashing the hash, we are encrypting the received sha256 hash using the RSA private key. Now I presume I need to use the public key to decrypt the hash, generate a sha256 hash of the original message and compare the two in order to verify.
Is there a method or function that is available in .NET that will let me do this? Can I accomplish this Using OpenSSL ? could you please point me how to accomplish verifying such a message as described above. thanks.