MacAlgorithmProvider MacAlgorithmProvider MacAlgorithmProvider MacAlgorithmProvider MacAlgorithmProvider Class


Represents a message authentication code (MAC). A MAC uses symmetric key cryptography to prevent message tampering. For more information, see MACs, hashes, and signatures.

public : sealed class MacAlgorithmProvider
struct winrt::Windows::Security::Cryptography::Core::MacAlgorithmProvider
public sealed class MacAlgorithmProvider
Public NotInheritable Class MacAlgorithmProvider
// This class does not provide a public constructor.

Windows 10 requirements

Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)


using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Storage.Streams;

namespace SampleMacAlgorithmProvider
    sealed partial class MacAlgProviderApp : Application
        public MacAlgProviderApp()
            // Initialize the application.

            // Initialize the hashing process.
            String strMsg = "This is a message to be authenticated";
            String strAlgName = MacAlgorithmNames.HmacSha384;
            IBuffer buffMsg;
            CryptographicKey hmacKey;
            IBuffer buffHMAC;

            // Create a hashed message authentication code (HMAC)
                out buffMsg,
                out hmacKey,
                out buffHMAC);

            // Verify the HMAC.

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

            // Demonstrate how to retrieve the name of the algorithm used.
            String strNameUsed = objMacProv.AlgorithmName;

            // 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.
            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");

        public void VerifyHMAC(
            IBuffer buffMsg,
            CryptographicKey hmacKey,
            IBuffer buffHMAC)
            // The input key must be securely shared between the sender of the HMAC and 
            // the recipient. The recipient uses the CryptographicEngine.VerifySignature() 
            // method as follows to verify that the message has not been altered in transit.
            Boolean IsAuthenticated = CryptographicEngine.VerifySignature(hmacKey, buffMsg, buffHMAC);
            if (!IsAuthenticated)
                throw new Exception("The message cannot be verified.");


You create a MacAlgorithmProvider object by calling the static OpenAlgorithm method and specifying one of the following algorithm names:

  • HMAC_MD5
  • HMAC_SHA256
  • HMAC_SHA384
  • HMAC_SHA512


AlgorithmName AlgorithmName AlgorithmName AlgorithmName AlgorithmName

Gets the name of the open MAC algorithm.

MacLength MacLength MacLength MacLength MacLength

Gets the length, in bytes, of the message authentication code.


CreateHash(IBuffer) CreateHash(IBuffer) CreateHash(IBuffer) CreateHash(IBuffer) CreateHash(IBuffer)

Creates a CryptographicHash object that supports incremental hash operations.

CreateKey(IBuffer) CreateKey(IBuffer) CreateKey(IBuffer) CreateKey(IBuffer) CreateKey(IBuffer)

Creates a symmetric key that can be used to create the MAC value.

OpenAlgorithm(String) OpenAlgorithm(String) OpenAlgorithm(String) OpenAlgorithm(String) OpenAlgorithm(String)

Creates a MacAlgorithmProvider object and opens the specified algorithm for use.

See also