Hash Functions

Work in progress. This note is still being written and incomplete.

A hash function HH maps a message mm of any length to a fixed nn-bit digest H(m)H(m). It is public and keyless.

A cryptographically secure hash function is:

  • Computationally efficient
    H(m)H(m) is fast to compute for any mm.
  • Deterministic
    The same mm always gives the same digest.
  • One-way
    Preimage resistant, defined below.
  • Collision resistant
    Defined below.

Strict Avalanche Criterion

Also called SAC or bit diffusion. When a single input bit is flipped, about 50% of the output bits flip on average, and the probability that any given output bit flips is independent of which input bit was flipped and of the input value.

Properties

Each property is stated as work an attacker cannot afford. HH is modelled to be a uniformly random function, for the below analysis.

Preimage Resistance

Given a digest dd, an attacker cannot find any mm with H(m)=dH(m) = d practically.

The attacker is aiming at one fixed digest dd. Each trial message lands on it with chance 11 in 2n2^n, so about 2n2^n trials are needed.

Weak Collision Resistance

Aka. second-preimage resistance. Given a fixed message mm, an attacker cannot find a different mm' with H(m)=H(m)H(m') = H(m).

Again one fixed target, the digest of the given mm. Each trial mm' lands on it with chance 11 in 2n2^n, so about 2n2^n trials are needed, the same as preimage resistance.

Strong Collision Resistance

An attacker cannot find any pair mmm \neq m' with H(m)=H(m)H(m) = H(m'), both of their choosing.

Any two of the attacker’s own messages sharing a digest is a win. Each new message can pair with every earlier one, so after hashing kk messages there are about k2/2k^2/2 candidate pairs. That many pairs cover the 2n2^n possible digests once k2n/2k \approx 2^{n/2}, so the work drops to the square root.

Same effect as the birthday problem: matching one given birthday needs about 365365 people, but finding any shared birthday needs only about 2323.

Uses

Cryptographic uses:

  • Padding
    Randomised padding schemes such as OAEP mix message bits with hash output before public key encryption.
  • Signing
    A signature is computed over H(m)H(m), so it covers an input of any length at fixed cost.
  • Key derivation
    A raw secret is hashed to produce a symmetric key of the required length with no algebraic structure.

In real world:

  • File integrity
    A published digest lets anyone check a downloaded file was not altered.
  • Blockchain security
    Each block stores the digest of the previous block, so any edit breaks the chain.
  • Password storage
    The database keeps H(password)H(\text{password}), not the password. Even if the database leaks, the attacker cannot reverse the digests back to plaintext passwords.

Examples

OAEP

Optimal Asymmetric Encryption Padding. A randomised padding applied to a message before RSA encryption.

It is a 2-round Feistel network over the message MM and a random seed rr, with round functions built from a hash HH:

  • maskedM=(Mpadding)G(r)\text{maskedM} = (M \,\|\, \text{padding}) \oplus G(r), where GG expands rr to the block width.
  • maskedR=rH(maskedM)\text{maskedR} = r \oplus H(\text{maskedM}).
  • The RSA input is maskedMmaskedR\text{maskedM} \,\|\, \text{maskedR}.

Effects:

  • Randomised
    The same MM encrypts differently each run through rr.
  • All-or-nothing
    Every output bit depends on every bit of MM and rr, so a tampered block fails the padding check on decryption.

This makes RSA encryption secure against chosen-ciphertext attacks. The hash is used only as padding, so no collision property is relied on.

Written by September 16, 2026 3 min read
Was this helpful?