Export (0) Print
Expand All

DSACryptoServiceProvider.SignHash Method

Computes the signature for the specified hash value by encrypting it with the private key.

Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)

public byte[] SignHash (
	byte[] rgbHash,
	string str
)
public byte[] SignHash (
	byte[] rgbHash, 
	String str
)
public function SignHash (
	rgbHash : byte[], 
	str : String
) : byte[]

Parameters

rgbHash

The hash value of the data to be signed.

str

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

Return Value

The DSA signature for the specified hash value.

Exception typeCondition

ArgumentNullException

The rgbHash parameter is a null reference (Nothing in Visual Basic).

CryptographicException

The cryptographic service provider (CSP) cannot be acquired.

-or-

There is no private key.

This method creates a digital signature that is verified using the VerifyHash method.

DSA uses the SHA1 hash algorithm.

The following code example signs and verifies data using the DSACryptoServiceProvider class.

using System;
using System.Security.Cryptography;

class DSACSPSample
{
		
	static void Main()
	{
		try
		{
			//Create a new instance of DSACryptoServiceProvider to generate
			//a new key pair.
			DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();

			//The hash to sign.
			byte[] Hash = {59,4,248,102,77,97,142,201,210,12,224,93,25,41,100,197,213,134,130,135};
			
			//Use the MapNameToOID method to get an OID 
			//for the SHA1 algorithm.
			string OID = CryptoConfig.MapNameToOID("SHA1");
			
			//The value to hold the signed hash.
			byte[] SignedHashValue = DSASignHash(Hash, DSA.ExportParameters(true), OID);

			//Verify the hash and display the results.
			if(DSAVerifyHash(Hash, SignedHashValue, DSA.ExportParameters(false), OID))
			{
				Console.WriteLine("The hash value was verified.");
			}
			else
			{
				Console.WriteLine("The hash value was not verified.");
			}


		}
		catch(ArgumentNullException e)
		{
			Console.WriteLine(e.Message);
		}
	}

	public static byte[] DSASignHash(byte[] HashToSign, DSAParameters DSAKeyInfo, string HashOID)
	{
		try
		{
			//Create a new instance of DSACryptoServiceProvider.
			DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();

			//Import the key information.   
			DSA.ImportParameters(DSAKeyInfo);

			//Sign the hash and return it.
			return DSA.SignHash(HashToSign, HashOID);
		}
		catch(CryptographicException e)
		{
			Console.WriteLine(e);

			return null;
		}

	}

	public static bool DSAVerifyHash(byte[] Hash, byte[] SignedHash, DSAParameters DSAKeyInfo, string HashOID)
	{
		try
		{
			//Create a new instance of DSACryptoServiceProvider.
			DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();

			//Import the key information. 
			DSA.ImportParameters(DSAKeyInfo);

			//Verify the signature and return the result.	
			return DSA.VerifyHash(Hash, HashOID, SignedHash);

		}
		catch(CryptographicException e)
		{
			Console.WriteLine(e.Message);

			return false;
		}
			
	}

}

import System .* ;
import System.Security.Cryptography .* ;

class DSACSPSample
{
    public static void main(String[] args)
    {
        try {
            // Create a new instance of DSACryptoServiceProvider to generate
            // a new key pair.
            DSACryptoServiceProvider dsa =  new DSACryptoServiceProvider();
            
            // The hash to sign.
            ubyte hash[] =  {59, 4, 248, 102, 77, 97, 142, 201, 210, 12, 224,
                93, 25, 41, 100, 197, 213, 134, 130, 135};
            
            // Use the MapNameToOID method to get an oid 
            // for the SHA1 algorithm.
            String oid = CryptoConfig.MapNameToOID("SHA1");
            
            // The value to hold the signed hash.
            ubyte signedHashValue[] = DSASignHash(hash, 
            dsa.ExportParameters(true), oid);
            
            // Verify the hash and display the results.
            if (DSAVerifyHash(hash, signedHashValue, 
                dsa.ExportParameters(false), oid)) {
                Console.WriteLine("The hash value was verified.");
            }
            else {
                Console.WriteLine("The hash value was not verified.");
            }
        }
        catch(ArgumentNullException e) {
            Console.WriteLine(e.get_Message());
        }
    } //main

    public static ubyte[] DSASignHash(ubyte hashToSign[], 
        DSAParameters dsaKeyInfo, String hashOid) 
    {
        try {
            //Create a new instance of DSACryptoServiceProvider.
            DSACryptoServiceProvider dsa =  new DSACryptoServiceProvider();
            
            //Import the key information.   
            dsa.ImportParameters(dsaKeyInfo);
            
            //Sign the hash and return it.
            return dsa.SignHash(hashToSign, hashOid) ;
        }
        catch(CryptographicException e) {
            Console.WriteLine(e);
            return null;
        }
    } //DSASignHash  
        
    public static boolean DSAVerifyHash(ubyte hash[], ubyte signedHash[],
        DSAParameters dsaKeyInfo, String hashOid) 
    {
        try {
            //Create a new instance of DSACryptoServiceProvider.
            DSACryptoServiceProvider dsa =  new DSACryptoServiceProvider();
            
            //Import the key information. 
            dsa.ImportParameters(dsaKeyInfo);
            
            //Verify the signature and return the result.    
            return dsa.VerifyHash(hash, hashOid, signedHash) ;
        } 
        catch (CryptographicException e) {
            Console.WriteLine(e.get_Message());
            return false ;
        }
    } //DSAVerifyHash
} //DSACSPSample

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2015 Microsoft