共用方式為


CryptographicEngine.Sign(CryptographicKey, IBuffer) 方法

定義

簽署數位內容。 如需詳細資訊,請參閱 MAC、雜湊和簽章

public:
 static IBuffer ^ Sign(CryptographicKey ^ key, IBuffer ^ data);
 static IBuffer Sign(CryptographicKey const& key, IBuffer const& data);
public static IBuffer Sign(CryptographicKey key, IBuffer data);
function sign(key, data)
Public Shared Function Sign (key As CryptographicKey, data As IBuffer) As IBuffer

參數

key
CryptographicKey

用於簽署的金鑰。

data
IBuffer

要簽署的資料。

傳回

資料的簽章。

範例

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;
}

備註

如果金鑰是保存的金鑰,而且作業需要 UI 或花費很長的時間,請改用 SignAsync 方法。

如需簽署數位資料的詳細資訊,請參閱 MAC、雜湊和簽章

適用於