The two methods are equivalent to signdata and verifydata, except that the input parameter is a hash value rather than the original data. That is to say, the RSA-encrypted hash function in signdata is self-calculated. While signhash requires us to calculate and then input parameters.
The only note is that the hash algorithm specified by the signhash (and verifyhash) method is a string object, but it is in the format of a hash algorithm identifier (OID. Therefore, another method is required: cryptoconfig. mapnameiid to convert a hash algorithm string into a hash algorithm identifier (OID.
Code:
// + Using system. Security. Cryptography
Using (var rsa = new rsacryptoserviceprovider ())
Using (VAR sha1 = sha1.create ())
{
// Raw data
VaR DATA = new byte [] {1, 2, 3 };
// Calculate the hash value
VaR hash = sha1.computehash (data );
// Use signhash. Use cryptoconfig. mapnametooid
VaR sighash = RSA. signhash (hash, cryptoconfig. mapnametooid ("sha1 "));
// Use signdata to pass in data directly (the hash value is calculated inside the function)
VaR sigdata = RSA. signdata (data, typeof (sha1 ));
// Output two signature data
Console. writeline (bitconverter. tostring (sighash ));
Console. writeline (bitconverter. tostring (sigdata ));
// Verify
Console. writeline (RSA. verifyhash (hash, "sha1", sighash ));
Console. writeline (RSA. verifydata (data, typeof (sha1), sigdata ));
}
Output:
Bytes
-Timeout
Slave-
Bytes
-69-97-d8-0c-f7-1d-01-ae-89-8c-be-fe-86-8c-81-24-aa-9d-8a-22-84
Bytes
-Timeout
Slave-
Bytes
-69-97-d8-0c-f7-1d-01-ae-89-8c-be-fe-86-8c-81-24-aa-9d-8a-22-84
True
True
The two methods can obtain the same signature data.