CSharpTest.Net
VerifyHash Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Crypto Namespace > RSAPublicKey Class : VerifyHash Method

signature
hashBytes

Glossary Item Box

Signs the provided Hash code with the private key and returns the hash signature

Syntax

Visual Basic (Declaration) 
Public Function VerifyHash( _
   ByVal signature() As Byte, _
   ByVal hashBytes As Hash _
) As Boolean
C# 
public bool VerifyHash( 
   byte[] signature,
   Hash hashBytes
)

Parameters

signature
hashBytes

Example

Library/Library.Test/TestRSACrypto.cs

C#Copy Code
byte[] data = new byte[100];
new Random().NextBytes(data);

foreach (HashAlgorithm ha in new HashAlgorithm[] { MD5.Create(), SHA1.Create(), SHA256.Create(), SHA384.Create(), SHA512.Create() })
{
    Hash hash = Hash.FromBytes(ha.ComputeHash(data));
    Assert.AreEqual(CryptoConfig.MapNameToOID(ha.GetType().FullName), hash.AlgorithmOID);

    using (RSAPrivateKey key = new RSAPrivateKey())
    {
        byte[] sig = key.SignHash(hash);

        using (RSAPublicKey pub = key.PublicKey)
        {
            Assert.IsTrue(pub.VerifyHash(sig, Hash.FromBytes(ha.ComputeHash(data))));
            data[0] = (byte)~data[0];
            Assert.IsFalse(pub.VerifyHash(sig, Hash.FromBytes(ha.ComputeHash(data))));
        }
    }
}
VB.NETCopy Code
Dim data As Byte() = New Byte(100) {}
New Random().NextBytes(data)

For Each ha As HashAlgorithm In New HashAlgorithm() {MD5.Create(), SHA1.Create(), SHA256.Create(), SHA384.Create(), SHA512.Create()}
    Dim hash As Hash = Hash.FromBytes(ha.ComputeHash(data))
    Assert.AreEqual(CryptoConfig.MapNameToOID(ha.[GetType]().FullName), hash.AlgorithmOID)

    Using key As New RSAPrivateKey()
        Dim sig As Byte() = key.SignHash(hash)

        Using pub As RSAPublicKey = key.PublicKey
            Assert.IsTrue(pub.VerifyHash(sig, Hash.FromBytes(ha.ComputeHash(data))))
            data(0) = DirectCast(Not data(0), Byte)
            Assert.IsFalse(pub.VerifyHash(sig, Hash.FromBytes(ha.ComputeHash(data))))
        End Using
    End Using
Next

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys