Export (0) Print
Expand All

DSACryptoServiceProvider.SignData Method (Stream)

Computes the hash value of the specified input stream and signs the resulting hash value.

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

public byte[] SignData (
	Stream inputStream
)
public byte[] SignData (
	Stream inputStream
)
public function SignData (
	inputStream : Stream
) : byte[]

Parameters

inputStream

The input data for which to compute the hash.

Return Value

The DSA signature for the specified data.

DSA uses the SHA1 hash algorithm.

The following code example creates a DSACryptoServiceProvider, generates a new key pair, and signs and tests some data.

using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;

class DSACSPSample
{
    static void Main()
    {
        try
        {
            ASCIIEncoding ByteConverter = new ASCIIEncoding();

            // Create some bytes to be signed.
            byte[] dataBytes = ByteConverter.GetBytes("Here is some data to sign!");
   
            // Create a buffer for the memory stream.
            byte[] buffer = new byte[dataBytes.Length];

            // Create a MemoryStream.
            MemoryStream mStream = new MemoryStream(buffer);

            // Write the bytes to the stream and flush it.
            mStream.Write(dataBytes, 0, dataBytes.Length);

            mStream.Flush();

            // Create a new instance of the DSACryptoServiceProvider class 
            // and automatically create a new key-pair.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider();

            // Export the key information to an DSAParameters object.
            // You must pass true to export the private key for signing.
            // However, you do not need to export the private key
            // for verification.
            DSAParameters Key = DSAalg.ExportParameters(true);

            // Hash and sign the data.
            byte[] signedData = HashAndSignBytes(mStream, Key);
           

            // Verify the data and display the result to the 
            // console.
            if(VerifySignedHash(dataBytes, signedData, Key))
            {
                Console.WriteLine("The data was verified.");
            }
            else
            {
                Console.WriteLine("The data does not match the signature.");
            } 
            
            // Close the MemoryStream.
            mStream.Close();

        }
        catch(ArgumentNullException)
        {
            Console.WriteLine("The data was not signed or verified");

        }
    }
    public static byte[] HashAndSignBytes(Stream DataStream, DSAParameters Key)
    {
        try
        { 
            // Reset the current position in the stream to 
            // the beginning of the stream (0). DSACryptoServiceProvider
            // can't verify the data unless the the stream position
            // is set to the starting position of the data.
            DataStream.Position = 0;

            // Create a new instance of DSACryptoServiceProvider using the 
            // key from DSAParameters.  
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider();

            DSAalg.ImportParameters(Key);

            // Hash and sign the data. Pass a new instance of SHA1CryptoServiceProvider
            // to specify the use of SHA1 for hashing.
            return DSAalg.SignData(DataStream);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

            return null;
        }
    }

    public static bool VerifySignedHash(byte[] DataToVerify, byte[] SignedData, DSAParameters Key)
    {
        try
        {
            // Create a new instance of DSACryptoServiceProvider using the 
            // key from DSAParameters.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider();

            DSAalg.ImportParameters(Key);

            // Verify the data using the signature.  Pass a new instance of SHA1CryptoServiceProvider
            // to specify the use of SHA1 for hashing.
            return DSAalg.VerifyData(DataToVerify, SignedData); 

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

            return false;
        }
    }
}

import System.*;
import System.Security.Cryptography.*;
import System.Text.*;
import System.IO.*;

class DSACSPSample
{
    public static void main(String[] args)
    {
        try {
            ASCIIEncoding byteConverter = new ASCIIEncoding();

            // Create some bytes to be signed.
            ubyte dataBytes[] = byteConverter.GetBytes(
                "Here is some data to sign!");

            // Create a buffer for the memory stream.
            ubyte buffer[] = new ubyte[dataBytes.length];

            // Create a MemoryStream.
            MemoryStream mStream = new MemoryStream(buffer);

            // Write the bytes to the stream and flush it.
            mStream.Write(dataBytes, 0, dataBytes.length);
            mStream.Flush();

            // Create a new instance of the DSACryptoServiceProvider class 
            // and automatically create a new key-pair.
            DSACryptoServiceProvider dsaAlg = new DSACryptoServiceProvider();

            // Export the key information to an DSAParameters object.
            // You must pass true to export the private key for signing.
            // However, you do not need to export the private key
            // for verification.
            DSAParameters key = dsaAlg.ExportParameters(true);

            // Hash and sign the data.
            ubyte signedData[] = HashAndSignBytes(mStream, key);

            // Verify the data and display the result to the 
            // console.
            if (VerifySignedHash(dataBytes, signedData, key)) {
                Console.WriteLine("The data was verified.");
            }
            else {
                Console.WriteLine("The data does not match the signature.");
            }

            // Close the MemoryStream.
            mStream.Close();
        }
        catch (ArgumentNullException exp) {
            Console.WriteLine("The data was not signed or verified");
        }
    } //main

    public static ubyte[] HashAndSignBytes(Stream dataStream, DSAParameters key)
    {
        try {
            // Reset the current position in the stream to 
            // the beginning of the stream (0). DSACryptoServiceProvider
            // can't verify the data unless the the stream position
            // is set to the starting position of the data.
            dataStream.set_Position(0);

            // Create a new instance of DSACryptoServiceProvider using the 
            // key from DSAParameters.  
            DSACryptoServiceProvider dsaAlg = new DSACryptoServiceProvider();

            dsaAlg.ImportParameters(key);

            // Hash and sign the data. Pass a new instance of SHA1CryptoServiceProvider
            // to specify the use of SHA1 for hashing.
            return dsaAlg.SignData(dataStream);
        }
        catch (CryptographicException e) {
            Console.WriteLine(e.get_Message());
            return null;
        }
    } //HashAndSignBytes

    public static boolean VerifySignedHash(ubyte dataToVerify[], 
        ubyte signedData[], DSAParameters key)
    {
        try {
            // Create a new instance of DSACryptoServiceProvider using the 
            // key from DSAParameters.
            DSACryptoServiceProvider dsaAlg = new DSACryptoServiceProvider();
            dsaAlg.ImportParameters(key);

            // Verify the data using the signature.  
            // Pass a new instance of SHA1CryptoServiceProvider
            // to specify the use of SHA1 for hashing.
            return dsaAlg.VerifyData(dataToVerify, signedData);
        }
        catch (CryptographicException e) {
            Console.WriteLine(e.get_Message());
            return false;
        }
    } //VerifySignedHash
} //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:
© 2014 Microsoft