[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)
Public Function SignData ( _ buffer As Byte() _ ) As Byte()
public byte[] SignData( byte[] buffer )
public: array<unsigned char>^ SignData( array<unsigned char>^ buffer )
member SignData : buffer:byte[] -> byte[]
Parameters
- buffer
- Type: System.Byte[]
The input data for which to compute the hash.
This method creates a digital signature that is verified using the VerifyData method.
The following code example signs and verifies data using the DSACryptoServiceProvider class.
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
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; } } }
#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 ); } }
.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 SP1Windows 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.