Hashing, MACs & digital signatures
Use hashes to verify integrity and explain how signatures prove origin.
By Invicly Tech, , in Cyber Security Fundamentals. About 10 minutes to read.
In short
A hash is a one-way fingerprint of data: the same input always gives the same output, but you cannot reverse it. Hashes verify integrity, MACs add a secret key, and digital signatures prove origin. Old algorithms like MD5 are retired because collisions are feasible.
Explained simply
A hash function takes any input, a word or a 10 GB file, and produces a short fixed-size fingerprint called a digest. SHA-256 always gives 256 bits, shown as 64 hexadecimal characters. The same input always gives the same digest, and changing even one letter gives a completely different one.
Hashes are one-way. You cannot turn a digest back into the original data. That makes them perfect for checking integrity: if the file you downloaded has the same SHA-256 as the one the publisher listed, the file was not changed or corrupted.
But anyone can compute a hash. An attacker who swaps a file can also swap the hash next to it. To prove who made something, you need a key. A MAC (message authentication code) uses a shared secret key. A digital signature uses a private key, and anyone can check it with the matching public key.
Some old hash functions are broken. Researchers can create two different files with the same MD5 or SHA-1 digest. This is called a collision, and it means those functions can no longer be trusted for security.
Press Play on a diagram to walk through it one step at a time.
- No key
- Detects change
- Anyone can compute
- SHA-256
- Shared secret key
- Detects change
- Proves shared secret
- HMAC
- Private key signs
- Public key verifies
- Proves origin
- RSA, ECDSA
- 1Download 3 files
ISO, SHA256SUMS, SHA256SUMS.gpg
- 2Get signing key
From the publisher's official site
- 3Verify signature
Proves SHA256SUMS is genuine
- 4Check hash
sha256sum -c against the ISO
- 5Install
Only if both checks pass
In depth
The three security properties of a hash
A secure hash must resist three kinds of attack. Preimage resistance: given a digest, you cannot find any input that produces it. Second-preimage resistance: given one input, you cannot find a different input with the same digest. Collision resistance: you cannot find any two inputs with the same digest, even if you choose both.
Collision resistance is the first to break, because of the birthday effect: for an n-bit hash you only need about 2^(n/2) tries to find some collision, not 2^n. For SHA-256 that is 2^128, far out of reach. For MD5 (128 bits) the generic limit is 2^64, and clever attacks made it much easier still.
Hashes also show the avalanche effect: change one input bit and about half the output bits flip. That is why digests of nearly identical files look unrelated.
- SHA-256, SHA-384, SHA-512 (SHA-2 family): fine today
- SHA-3 and BLAKE2/BLAKE3: modern alternatives
- MD5 and SHA-1: broken for collisions
Why MD5 and SHA-1 were retired
Practical MD5 collisions were shown in 2004. By 2008 researchers used them to create a rogue CA certificate, and in 2012 the Flame espionage malware used an MD5 collision to forge a Microsoft code-signing certificate so its updates looked genuine. With MD5, an attacker can make a harmless file and a malicious file with the same digest, get the harmless one approved or signed, then swap them.
SHA-1 followed. In 2017 Google and CWI Amsterdam published SHAttered, two different PDF files with the same SHA-1 hash. In 2020 a cheaper chosen-prefix collision made attacks more realistic. Browsers stopped trusting SHA-1 certificates in 2017, and NIST plans to stop SHA-1 use entirely by the end of 2030.
MD5 is still fine for non-security jobs, such as spotting accidental corruption or de-duplicating files. For anything an attacker could influence, use SHA-256 or better.
MACs and HMAC in practice
A MAC proves two things: the message was not changed, and it came from someone who knows the shared key. HMAC is the standard way to build one from a hash: HMAC-SHA256 mixes the key into the hash in two passes.
Why not just hash key plus message? SHA-256 and other SHA-2 hashes allow a length extension attack: from SHA256(key || message) an attacker can compute a valid digest for the message with extra data added, without knowing the key. HMAC's design blocks this.
A very common real use is webhooks. GitHub, Stripe and many others sign each request body with HMAC-SHA256 and a secret you share with them, and send the result in a header (GitHub uses X-Hub-Signature-256). Your server recomputes it and compares. Use a constant-time comparison, such as hmac.compare_digest in Python, so attackers cannot learn the correct value byte by byte from timing.
- 1Sender signs
HMAC-SHA256(secret, body)
- 2Sends request
Body plus signature header
- 3Read raw body
Exact bytes, before JSON parsing
- 4Recompute HMAC
Same secret, same algorithm
- 5Constant-time compare
hmac.compare_digest
- 6Accept or reject
Mismatch: 401 and log it
How digital signatures work
A signature uses an asymmetric key pair. To sign, software hashes the message and then uses the private key to produce a signature over that hash. To verify, anyone with the public key checks that the signature matches the message. If one bit of the message changes, or the signature was made with a different key, verification fails.
This gives what a MAC cannot: non-repudiation. With a MAC, both sides share the key, so either could have created the tag. With a signature, only the private key holder could have signed.
Signatures protect software updates, code (Windows Authenticode, Apple notarization), Git commits and tags, container images (for example with Sigstore cosign), and TLS certificates. Common algorithms are RSA with PSS padding, ECDSA and Ed25519. For a download, the usual pattern is: the publisher signs a checksum file, you verify the signature, then check the file's hash against it.
- Hash: integrity only, no key
- MAC: integrity + shared-secret origin
- Signature: integrity + provable origin with public key
Learn it in 4 short steps
About 40m each. Track them with a timer in your plan.
- 1Explain one-way hash properties~40m
- 2Verify a file with SHA-256~40m
- 3Compare hashes, MACs and signatures~40m
- 4Explain why MD5 and SHA-1 are retired~40m
Key points
- A hash is deterministic and one-way.
- Hashes verify that data was not altered.
- A MAC uses a shared key; a signature uses a private key.
- MD5 and SHA-1 are broken for security use.
Key terms
Tap a card to flip it. 0/4 known
Examples
Verifying an Ubuntu ISO properly
Checking only the hash proves the file matches a list, but not that the list is genuine. Step 1: download the ISO plus SHA256SUMS and SHA256SUMS.gpg from the same release folder. Step 2: import Ubuntu's signing key (fingerprint published on ubuntu.com). Step 3: verify the signature on SHA256SUMS; look for "Good signature". Step 4: check the ISO against the list. --ignore-missing skips ISOs you did not download. On Windows, Get-FileHash file.iso -Algorithm SHA256 gives the hash.
# after importing the Ubuntu signing key with gpg --recv-keys <fingerprint>
gpg --keyid-format long --verify SHA256SUMS.gpg SHA256SUMS
# gpg: Good signature from "Ubuntu CD Image Automatic Signing Key ..."
sha256sum -c SHA256SUMS --ignore-missing
# ubuntu-24.04-live-server-amd64.iso: OKChecking a webhook signature in Python
A payment provider sends events to your API. Step 1: you and the provider share a secret. Step 2: the provider sends the body plus a header holding sha256= followed by the hex digest. Step 3: your code recomputes HMAC-SHA256 over the exact raw bytes of the body, not a re-serialised JSON object, because any change in spacing changes the hash. Step 4: compare with hmac.compare_digest. Step 5: reject anything that does not match, and log it: repeated failures may mean someone is probing your endpoint.
import hmac, hashlib
SECRET = b"whsec_example_shared_secret"
def sign(body: bytes) -> str:
return "sha256=" + hmac.new(SECRET, body, hashlib.sha256).hexdigest()
def verify(body: bytes, header: str) -> bool:
return hmac.compare_digest(sign(body), header)
body = b'{"event":"paid","amount":100}'
header = sign(body) # what the sender would send
print(verify(body, header)) # True
print(verify(body.replace(b"100", b"999"), header)) # FalseQuick reference
- sha256sum file (Linux), shasum -a 256 file (macOS), Get-FileHash file (PowerShell)
- sha256sum -c SHA256SUMS --ignore-missing checks downloads against a list
- Hash = integrity; MAC = integrity + shared key; signature = integrity + provable origin
- Use HMAC, never hash(key + message), to avoid length extension attacks
- Compare MACs with hmac.compare_digest (constant time), not ==
- MD5 and SHA-1 are broken for collisions; use SHA-256 or SHA-3
- Fast hashes are wrong for passwords; use Argon2id, scrypt or bcrypt
- A hash of a file is only trustworthy if the hash itself came from a trusted, signed source
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Hash the words hello and Hello with SHA-256 and compare the results. Then hash a 1-byte file and a large file and compare the digest lengths.
- Exercise 2medium
Create an Ed25519 key pair with openssl, sign a text file, verify it, then change one character and verify again.
- Exercise 3hard
Write a Python script that builds a SHA-256 baseline of every file in a folder, then later reports files that were added, removed or changed (a tiny file integrity monitor).
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Can you decrypt a SHA-256 hash?
- No, hashing is not encryption and there is no key. Attackers can only guess inputs and compare, which is why short or common passwords stored with fast hashes are easy to crack.
- Is MD5 OK for checking file downloads?
- It detects accidental corruption, but not deliberate tampering, because attackers can craft collisions. Use SHA-256 and check a signed checksum file.
- What is the difference between SHA-2 and SHA-3?
- They are different designs from NIST. SHA-2 (for example SHA-256) is the most widely used and still secure; SHA-3 uses a different internal structure and is immune to length extension attacks.