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

hashBytes

Glossary Item Box

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

Syntax

Visual Basic (Declaration) 
Public Function SignHash( _
   ByVal hashBytes As Hash _
) As Byte()
C# 
public byte[] SignHash( 
   Hash hashBytes
)

Parameters

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