Contents

HashFunction

A type that performs cryptographically secure hashing.

Declaration

@preconcurrency protocol HashFunction : Sendable

Overview

The 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, SHA384, or 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 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

Computing a hash in one call

Computing a hash iteratively

Inspecting hash information

See Also

Cryptographically secure hashes