Experiments and implementations · Intermediate

Compare hashes and MACs for integrity

Compare a trusted reference digest with a replaceable message-and-digest pair, then verify HMAC and its replay limitation.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Ask who protects the comparison value
  2. Predict both tampering conditions
  3. Execute all cases with standard APIs
  4. Separate shared-key authentication from other properties
  5. Define exactly which bytes are authenticated
  6. Hold the verification key fixed when testing changed bytes
  7. Exercise: explain replay and verification failure
  8. Ask Logeia about this articleCheck your understanding

Ask who protects the comparison value

Does a hash shipped beside a file prevent tampering? Separate a trusted reference digest from a situation where both message and digest can be replaced. Then compute HMAC with a shared key and compare verification for the original message, a changed message, and a different key. The program uses only Python 3.12 hashlib, hmac, and secrets, with fictional byte strings and no external service or real transaction.

Read the hash, MAC, and symmetric-cryptography overview first. This is not an implementation of a hash function or a new keyed-hash construction. Use the standard HMAC-SHA-256 API to test what each trust assumption permits you to detect. A fresh experimental key is generated on every run, is never printed, and is used only for the fixture messages.

Predict both tampering conditions

The original bytes contain item=book&amount=100; the changed bytes use amount=900. If the original digest is retained through a trusted channel, the changed message's digest differs and the modification is detected. This observation concerns these particular inputs. It is not a mathematical proof that a hash function has no collisions.

Now recompute the digest of the changed message and replace the comparison digest too. A receiver hashing that changed message sees a match. The hash function has not malfunctioned; the trusted-reference assumption disappeared. A digest matching a file downloaded from the same replaceable location does not by itself authenticate the publisher. A trusted distribution channel, signature, or other independent basis is needed to support that stronger claim.

Execute all cases with standard APIs

Save the program as mac_boundary.py and run python3.12 mac_boundary.py. After the two SHA-256 comparisons, it generates a random 32-byte shared key and computes HMAC. The verify function recomputes the expected tag and compares it using hmac.compare_digest. Notice the dedicated comparison API rather than relying on early-exit behavior from ordinary string comparison.

All assertions passed with Python 3.12.14. The original message succeeds with the same key. A changed message, a different key, and an ordinary hash substituted as a tag are rejected. Repeating the original message and tag succeeds again. That last success is intentional evidence that a MAC alone does not establish request freshness. The program prints condition results rather than secret keys or raw tags, keeping the record focused on the tested contract.

python
import hashlib
import hmac
import secrets

message = b'item=book&amount=100'
changed = b'item=book&amount=900'
digest = hashlib.sha256(message).digest()
assert hashlib.sha256(changed).digest() != digest
replacement_digest = hashlib.sha256(changed).digest()
assert hashlib.sha256(changed).digest() == replacement_digest
key = secrets.token_bytes(32)
other_key = secrets.token_bytes(32)
tag = hmac.digest(key, message, 'sha256')
def verify(candidate, candidate_tag, verification_key):
    expected = hmac.digest(verification_key, candidate, 'sha256')
    return hmac.compare_digest(expected, candidate_tag)
assert verify(message, tag, key)
assert not verify(changed, tag, key)
assert not verify(message, tag, other_key)
assert not verify(changed, replacement_digest, key)
assert verify(message, tag, key)  # A replay still has a valid MAC.
print('hash detects change when original digest is trusted')
print('replacing message and untrusted hash passes a hash comparison')
print('HMAC: original accepted, changed/wrong-key/hash rejected; replay accepted')

Separate shared-key authentication from other properties

Without the key, changing the message and recomputing an ordinary hash does not provide the same ability to produce a valid HMAC. The verifier must hold the correct key securely for that distinction to matter. A key that can be replaced freely alongside the message does not establish trust. Key distribution and storage are separate designs beyond this calculation fixture.

Anyone who knows the shared key can create valid tags. When several verifiers share it, a valid tag cannot independently establish to a third party which individual created it. That differs from the role of public-key signatures. A MAC also does not encrypt the message: the fixture bytes remain readable. Keep confidentiality, integrity, peer authentication, and freshness as separate properties instead of describing them all as “encrypted.”

Define exactly which bytes are authenticated

A MAC authenticates the supplied bytes. Text that looks equivalent may differ in encoding, whitespace, line endings, or JSON key order. A real protocol must define an unambiguous representation of whichever elements require protection: method, target, body, version, time, or other fields. A value omitted from the authenticated input is not protected against modification by this check.

The fixture uses fixed ASCII bytes and does not invent a general canonicalization scheme. Real integrations should follow a specified protocol and established implementation. Using compare_digest also does not prove the entire application leaks no timing information. Tag-length handling, error responses, and surrounding work may remain observable. Explain the comparison API's role separately from the conditions required for system-wide resistance to side-channel observations.

Hold the verification key fixed when testing changed bytes

The modification test retains the original verification key. Changing both message and key would leave the cause of rejection ambiguous. The wrong-key test instead keeps the original message and changes only the key. Although both cases reject, separating the changed assumptions explains two different roles in authentication. Record those roles rather than merging every negative outcome into “tampered.” The experiment can establish these relationships without printing tags or keys. In a review, the assertion inputs are often more informative than the final generic success line because they identify precisely which condition was varied.

Exercise: explain replay and verification failure

Why do two submissions of the original request both have valid MACs? The bytes and key are unchanged, so the verification calculation is unchanged. Rejecting a second use requires a suitable freshness rule, such as request identifiers, sequence numbers, or expiry, together with server-side state where needed. Merely appending a timestamp does not automatically enforce one-time use.

Next ask whether failure under other_key proves that the message was modified. It does not: the correct-verification-key assumption was changed, so modification is not the only explanation. Record separate columns for changed input, changed key, and trusted comparison material. Apply the same distinction to software-update signatures and certificate validation: successful computation and the basis for trusting its inputs are related but separate questions.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 20

Choose a learning path