<!--
{
  "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/HashFunction",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Apple CryptoKit"
    ],
    "preciseIdentifier" : "s:9CryptoKit12HashFunctionP"
  },
  "title" : "HashFunction"
}
-->

# HashFunction

A type that performs cryptographically secure hashing.

```
@preconcurrency protocol HashFunction : Sendable
```

## Overview

The [`HashFunction`](/documentation/CryptoKit/HashFunction) protocol describes an interface for computing a
fixed-length digest from an arbitrarily large collection of bytes. Because
the digest is small, you can quickly compare the digests to detect a
difference in two corresponding data sets. Alternatively, transmit or store
data with its digest to detect changes introduced after initially
calculating the digest.

Use one of the protocol’s adopters, like [`SHA256`](/documentation/CryptoKit/SHA256), [`SHA384`](/documentation/CryptoKit/SHA384), or
[`SHA512`](/documentation/CryptoKit/SHA512), to output a digest whose value varies significantly over even
small differences in the input data.

Checking a digest doesn’t guard against changes made by a malicious user who
also changes the digest to match. To handle this, compute a message
authentication code (MAC) like [`HMAC`](/documentation/CryptoKit/HMAC) instead. MACs rely on hashing, but
incorporate a secret cryptographic key into the digest computation. Only a
user that has the key can generate a valid MAC.

## Topics

### Specifying the output type

[`Digest`](/documentation/CryptoKit/HashFunction/Digest)

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

A type that represents the output of a hash.

### Computing a hash in one call

[`hash(data:)`](/documentation/CryptoKit/HashFunction/hash(data:))

Computes the digest of the bytes in the given data instance and
returns the computed digest.

### Computing a hash iteratively

[`init()`](/documentation/CryptoKit/HashFunction/init())

Creates a hash function.

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

Incrementally updates the hash function with the given data.

[`update(bufferPointer:)`](/documentation/CryptoKit/HashFunction/update(bufferPointer:))

Incrementally updates the hash function with the contents of the buffer.

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

Finalizes the hash function and returns the computed digest.

### Inspecting hash information

[`blockByteCount`](/documentation/CryptoKit/HashFunction/blockByteCount)

The number of bytes that represents the hash function’s internal state.

## Relationships

### Conforming Types

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

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

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

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

[`MD5`](/documentation/CryptoKit/Insecure/MD5)

[`SHA1`](/documentation/CryptoKit/Insecure/SHA1)

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

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

### Inherits From

[`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)