Skip to main content
CryptographicEngine.Sign | sign method

Applies to Windows and Windows Phone

Signs digital content. For more information, see MACs, Hashes, and Signatures.

Syntax


var iBuffer = Windows.Security.Cryptography.Core.CryptographicEngine.sign(key, data);

public static IBuffer Sign(
  CryptographicKey key, 
  IBuffer data
)

Public Shared Function Sign(
  key As CryptographicKey,  
  data As IBuffer 
) As IBuffer

public:
static IBuffer^ Sign(
  CryptographicKey^ key, 
  IBuffer^ data
)

Parameters

key

Type: CryptographicKey

Key used for signing.

data

Type: IBuffer

Data to be signed.

Return value

Type: IBuffer

The data's signature.

Remarks

If the key is a persisted key and the operation requires UI or takes a long time, use the SignAsync method instead.

For more information about signing digital data, see Hashes and Digital Signatures.

Examples


public IBuffer SampleCreateHMAC(
    String strMsg,
    String strAlgName,
    out IBuffer buffMsg,
    out CryptographicKey hmacKey)
{
    // Create a MacAlgorithmProvider object for the specified algorithm.
    MacAlgorithmProvider objMacProv = MacAlgorithmProvider.OpenAlgorithm(strAlgName);

    // Create a buffer that contains the message to be signed.
    BinaryStringEncoding encoding = BinaryStringEncoding.Utf8;
    buffMsg = CryptographicBuffer.ConvertStringToBinary(strMsg, encoding);

    // Create a key to be signed with the message.
    IBuffer buffKeyMaterial = CryptographicBuffer.GenerateRandom(objMacProv.MacLength);
    hmacKey = objMacProv.CreateKey(buffKeyMaterial);

    // Sign the key and message together.
    IBuffer buffHMAC = CryptographicEngine.Sign(hmacKey, buffMsg);

    // Verify that the HMAC length is correct for the selected algorithm
    if (buffHMAC.Length != objMacProv.MacLength)
    {
        throw new Exception("Error computing digest");
    }

    // Return the HMAC.
    return buffHMAC;
}

Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.Security.Cryptography.Core
Windows::Security::Cryptography::Core [C++]

Metadata

Windows.winmd

See also

CryptographicEngine