<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CryptoKit",
  "identifier" : "/documentation/CryptoKit/HMAC",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Apple CryptoKit"
    ],
    "preciseIdentifier" : "s:9CryptoKit4HMACV"
  },
  "title" : "HMAC"
}
-->

# HMAC

A hash-based message authentication algorithm.

```
struct HMAC<H> where H : HashFunction
```

## Overview

Use hash-based message authentication to create a code with a value that’s
dependent on both a block of data and a symmetric cryptographic key. Another
party with access to the data and the same secret key can compute the code
again and compare it to the original to detect whether the data changed.
This serves a purpose similar to digital signing and verification, but
depends on a shared symmetric key instead of public-key cryptography.

As with digital signing, the data isn’t hidden by this process. When you
need to encrypt the data as well as authenticate it, use a cipher like
[`AES`](/documentation/CryptoKit/AES) or [`ChaChaPoly`](/documentation/CryptoKit/ChaChaPoly) to put the data into a sealed box (an instance of
[`AES.GCM.SealedBox`](/documentation/CryptoKit/AES/GCM/SealedBox) or [`ChaChaPoly.SealedBox`](/documentation/CryptoKit/ChaChaPoly/SealedBox)).

## Topics

### Getting a key

[`Key`](/documentation/CryptoKit/HMAC/Key)

An alias for the symmetric key type used to compute or verify a message
authentication code.

[`SymmetricKey`](/documentation/CryptoKit/SymmetricKey)

A symmetric cryptographic key.

### Working with codes

[`MAC`](/documentation/CryptoKit/HMAC/MAC)

An alias for a hash-based message authentication code.

[`HashedAuthenticationCode`](/documentation/CryptoKit/HashedAuthenticationCode)

A hash-based message authentication code.

[`MessageAuthenticationCode`](/documentation/CryptoKit/MessageAuthenticationCode)

A type that represents a message authentication code.

### Creating an authentication code with one call

### Creating an authentication code iteratively

[`init(key:)`](/documentation/CryptoKit/HMAC/init(key:))

Creates a message authentication code generator.

[`update(data:)`](/documentation/CryptoKit/HMAC/update(data:))

Updates the message authentication code computation with a block of
data.

[`finalize()`](/documentation/CryptoKit/HMAC/finalize())

Finalizes the message authentication computation and returns the
computed code.

### Checking an authentication code

[`isValidAuthenticationCode(_:authenticating:using:)`](/documentation/CryptoKit/HMAC/isValidAuthenticationCode(_:authenticating:using:)-8ezmw)

Returns a Boolean value indicating whether the given message
authentication code is valid for a block of data.

[`isValidAuthenticationCode(_:authenticating:using:)`](/documentation/CryptoKit/HMAC/isValidAuthenticationCode(_:authenticating:using:)-5jbc8)

Returns a Boolean value indicating whether the given message
authentication code is valid for a block of data stored in a buffer.

[`isValidAuthenticationCode(_:authenticating:using:)`](/documentation/CryptoKit/HMAC/isValidAuthenticationCode(_:authenticating:using:)-5ilt9)

Returns a Boolean value indicating whether the given message
authentication code represented as contiguous bytes is valid for a block
of data.

## Relationships

### Conforms To

[`Sendable`](/documentation/Swift/Sendable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)