DSACryptoServiceProvider.SignData Method (Byte[]) (System.Security.Cryptography)

Switch View :
ScriptFree
.NET Framework Class Library
DSACryptoServiceProvider.SignData Method (Byte[])

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Computes the hash value of the specified byte array and signs the resulting hash value.

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

Visual Basic
Public Function SignData ( _
	buffer As Byte() _
) As Byte()
C#
public byte[] SignData(
	byte[] buffer
)
Visual C++
public:
array<unsigned char>^ SignData(
	array<unsigned char>^ buffer
)
F#
member SignData : 
        buffer:byte[] -> byte[] 

Parameters

buffer
Type: System.Byte[]

The input data for which to compute the hash.

Return Value

Type: System.Byte[]
The DSA signature for the specified data.
Remarks

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

DSA uses the SHA1 hash algorithm.

Examples

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

Visual Basic

Imports System
Imports System.Security.Cryptography

 _

Class DSACSPSample


    Shared Sub Main()
        Try
            'Create a new instance of DSACryptoServiceProvider to generate
            'a new key pair.
            Dim DSA As New DSACryptoServiceProvider()

            'The data to sign.
            Dim Data As Byte() = {59, 4, 248, 102, 77, 97, 142, 201, 210, 12, 224, 93, 25, 41, 100, 197, 213, 134, 130, 135}

            'The value to hold the signed value.
            Dim SignedValue As Byte() = DSASignData(Data, DSA.ExportParameters(True))

            'Verify the data and display the results.
            If DSAVerifyData(Data, SignedValue, DSA.ExportParameters(False)) Then
                Console.WriteLine("The hash value was verified.")
            Else
                Console.WriteLine("The hash value was not verified.")
            End If


        Catch e As ArgumentNullException
            Console.WriteLine(e.Message)
        End Try
    End Sub


    Public Shared Function DSASignData(ByVal DataToSign() As Byte, ByVal DSAKeyInfo As DSAParameters) As Byte()
        Try
            'Create a new instance of DSACryptoServiceProvider.
            Dim DSA As New DSACryptoServiceProvider()

            'Import the key information.   
            DSA.ImportParameters(DSAKeyInfo)

            'Compute hash value, sign the hash, and return the signed hash.
            Return DSA.SignData(DataToSign)
        Catch e As CryptographicException
            Console.WriteLine(e.Message)

            Return Nothing
        End Try
    End Function


    Public Shared Function DSAVerifyData(ByVal Data() As Byte, ByVal SignedData() As Byte, ByVal DSAKeyInfo As DSAParameters) As Boolean
        Try
            'Create a new instance of DSACryptoServiceProvider.
            Dim DSA As New DSACryptoServiceProvider()

            'Import the key information. 
            DSA.ImportParameters(DSAKeyInfo)

            'Verify the signature and return the result.	
            Return DSA.VerifyData(Data, SignedData)

        Catch e As CryptographicException
            Console.WriteLine(e.Message)

            Return False
        End Try
    End Function
End Class



C#

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 data to sign.
			byte[] Data = {59,4,248,102,77,97,142,201,210,12,224,93,25,41,100,197,213,134,130,135};
				
			//The value to hold the signed value.
			byte[] SignedValue = DSASignData(Data, DSA.ExportParameters(true));

			//Verify the data and display the results.
			if(DSAVerifyData(Data, SignedValue, DSA.ExportParameters(false)))
			{
				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[] DSASignData(byte[] DataToSign, DSAParameters DSAKeyInfo)
	{
		try
		{
			//Create a new instance of DSACryptoServiceProvider.
			DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();

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

			//Compute hash value, sign the hash, and return the signed hash.
			return DSA.SignData(DataToSign);
		}
		catch(CryptographicException e)
		{
			Console.WriteLine(e.Message);

			return null;
		}

	}

	public static bool DSAVerifyData(byte[] Data, byte[] SignedData, DSAParameters DSAKeyInfo)
	{
		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.VerifyData(Data, SignedData);

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

			return false;
		}
			
	}

}


Visual C++

#using <System.dll>

using namespace System;
using namespace System::Security::Cryptography;
array<Byte>^ DSASignData( array<Byte>^DataToSign, DSAParameters DSAKeyInfo )
{
   try
   {

      //Create a new instance of DSACryptoServiceProvider.
      DSACryptoServiceProvider^ DSA = gcnew DSACryptoServiceProvider;

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

      //Compute hash value, sign the hash, and return the signed hash.
      return DSA->SignData( DataToSign );
   }
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
      return nullptr;
   }

}

bool DSAVerifyData( array<Byte>^Data, array<Byte>^SignedData, DSAParameters DSAKeyInfo )
{
   try
   {

      //Create a new instance of DSACryptoServiceProvider.
      DSACryptoServiceProvider^ DSA = gcnew DSACryptoServiceProvider;

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

      //Verify the signature and return the result.    
      return DSA->VerifyData( Data, SignedData );
   }
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
      return false;
   }

}

int main()
{
   try
   {

      //Create a new instance of DSACryptoServiceProvider to generate
      //a new key pair.
      DSACryptoServiceProvider^ DSA = gcnew DSACryptoServiceProvider;

      //The data to sign.
      array<Byte>^Data = {59,4,248,102,77,97,142,201,210,12,224,93,25,41,100,197,213,134,130,135};

      //The value to hold the signed value.
      array<Byte>^SignedValue = DSASignData( Data, DSA->ExportParameters( true ) );

      //Verify the data and display the results.
      if ( DSAVerifyData( Data, SignedValue, DSA->ExportParameters( false ) ) )
      {
         Console::WriteLine( "The hash value was verified." );
      }
      else
      {
         Console::WriteLine( "The hash value was not verified." );
      }
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}



Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8 Consumer Preview, Windows Server 8 Beta, Windows 7, Windows Server 2008 SP2, Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

See Also

Reference

Other Resources