. Net (C #): signhash and verifyhash of RSA

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.