RSACryptoServiceProvider.VerifyData(Byte[], Object, Byte[]) Method

Definition

Verifies that a digital signature is valid by determining the hash value in the signature using the provided public key and comparing it to the hash value of the provided data.

C#
public bool VerifyData(byte[] buffer, object halg, byte[] signature);

Parameters

buffer
Byte[]

The data that was signed.

halg
Object

The name of the hash algorithm used to create the hash value of the data.

signature
Byte[]

The signature data to be verified.

Returns

true if the signature is valid; otherwise, false.

Exceptions

The halg parameter is null.

The halg parameter is not a valid type.

Examples

The following example shows how to use the VerifyData method to verify a signature. This code example is part of a larger example provided for the SignHash method.

C#
public bool VerifyHash(RSAParameters rsaParams, byte[] signedData, byte[] signature)
{
    RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();
    SHA1Managed hash = new SHA1Managed();
    byte[] hashedData;

    rsaCSP.ImportParameters(rsaParams);
    bool dataOK = rsaCSP.VerifyData(signedData, CryptoConfig.MapNameToOID("SHA1"), signature);
    hashedData = hash.ComputeHash(signedData);
    return rsaCSP.VerifyHash(hashedData, CryptoConfig.MapNameToOID("SHA1"), signature);
}

Remarks

This method verifies the RSA digital signature produced by the SignData method. The signature is verified by obtaining the hash value from the signature using the public key it was signed with, and comparing that value to the hash value of the provided data.

The halg parameter can accept a String, a HashAlgorithm, or a Type.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also