TExcelTAD
Home › Applied Cryptography: Ciphers, Hashes, Signatures, PKI & TLS › SHA-2, SHA-3, BLAKE2 and the Avalanche Effect

SHA-2, SHA-3, BLAKE2 and the Avalanche Effect

When a security engineer needs a hash today, there are three families to choose from: SHA-2 (the workhorse, in every certificate and TLS handshake), SHA-3 (a different design, standardised as a backup), and BLAKE2/BLAKE3 (fast software hashes with a built-in key). This lesson shows how they differ, what they have in common, and one property you can watch with your own eyes: the avalanche effect.

The lab: one bit in, half the bits out

Pick a message and flip one input bit. The lab computes SHA-256 of both versions (the real FIPS 180-4 algorithm) and marks every output bit that changed, next to a toy checksum (the sum of the bytes) for comparison.

m = "abc"m′ = "ebc" (bit 5: byte 0, 61 → 65)SHA-256(m)SHA-256(m′)130 of 256 bits changed(50.8 %; ideal 128 ± 8)SHA-256(m)ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015adSHA-256(m′)05011ba86cf326806eda4aa8bd9c60bb5609b2bbcb65e1b8e8f72e576ca62dce10changed bittoy checksum: sum of bytes mod 65536 (16 bits)sum(m) = 0126sum(m′) = 012a2 of 16 bits changed: the change is predictablebits changed, for input bits 0–2396128160

For "abc" with bit 5 flipped, 130 of the 256 output bits change; sweep the bit slider and every flip lands near 128, the expected value for independent random bits (with a typical spread of about ±8). The checksum, by contrast, changes one bit plus a few carries: from its output you can tell what was changed. A hash whose output changed predictably would let an attacker steer it, so avalanche is a basic sanity property, though not a proof of security.

The families

SHA-2 SHA-3 BLAKE2
standard FIPS 180-4 (2015 edition; SHA-256 dates from 2001) FIPS 202 (2015) RFC 7693 (2015)
design Merkle–Damgård with a Davies–Meyer compression function Keccak sponge: absorb input into a 1600-bit state, squeeze output HAIFA-style, built on ChaCha's mixing function
members SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/256 SHA3-224/256/384/512, extendable-output SHAKE128/256 BLAKE2b (64-bit, up to 64 bytes), BLAKE2s (32-bit, up to 32 bytes)
length extension yes for SHA-256, SHA-512 no no
keyed mode via HMAC KMAC (SP 800-185) built in (key=)
speed in software fast; very fast with SHA-NI instructions slower in software, fast in hardware faster than SHA-2 without hardware help

SHA-3 came out of a public NIST competition (2007–2012) started after the 2004–2005 attacks on MD5 and SHA-1, so that a structurally different hash would be ready if SHA-2 fell. SHA-2 did not fall; both are approved and both are fine. BLAKE2 was a SHA-3 finalist's successor; BLAKE3 (2020) is faster still and parallel, but not in Python's standard library.

All of them, from Python

python · try it yourself
import hashlib
m = b"abc"
for name in ("md5", "sha1", "sha256", "sha512", "sha3_256", "blake2b", "blake2s"):
    d = hashlib.new(name, m).digest()
    print(f"{name:9} {len(d)*8:3} bits  {d.hex()[:24]}...")
print("shake_128, 20 bytes:", hashlib.shake_128(m).hexdigest(20))
# BLAKE2 has a built-in key: a fast MAC without HMAC's two passes.
print("keyed blake2b-256 :", hashlib.blake2b(m, key=b"lab-key", digest_size=32).hexdigest()[:24], "...")
# Streaming: update() in pieces gives the same digest as one call
s = hashlib.sha256(); s.update(b"a"); s.update(b"bc")
print("streamed == one-shot:", s.hexdigest() == hashlib.sha256(m).hexdigest())
md5       128 bits  900150983cd24fb0d6963f7d...
sha1      160 bits  a9993e364706816aba3e2571...
sha256    256 bits  ba7816bf8f01cfea414140de...
sha512    512 bits  ddaf35a193617abacc417349...
sha3_256  256 bits  3a985da74fe225b2045c172d...
blake2b   512 bits  ba80a53f981c4d0d6a2797b6...
blake2s   256 bits  508c5e8c327c14e2e1a72ba3...
shake_128, 20 bytes: 5881092dd818bf5cf8a3ddb793fbcba74097d5c5
keyed blake2b-256 : 384bc595ccd97c9577aad261 ...
streamed == one-shot: True

ba7816bf… for SHA-256 and 3a985da7… for SHA3-256 are the published FIPS test values for "abc": SHA-3 is not "SHA-2 with a bigger number", it is a different function with a completely different output. SHAKE128 produces as many bytes as you ask for. MD5 and SHA-1 are still in hashlib for compatibility; the next lesson explains why they must not be used where collisions matter.

The same digests from OpenSSL and coreutils in the sandbox, as you would compute them during an incident:

bash · try it yourself
printf 'abc' > abc.txt
for a in sha256 sha3-256 blake2s256 sha512-256; do openssl dgst -$a abc.txt; done
printf 'abc' | openssl dgst -sha256 -hmac 'lab-key'
sha256sum abc.txt
SHA2-256(abc.txt)= ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
SHA3-256(abc.txt)= 3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532
BLAKE2S-256(abc.txt)= 508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982
SHA2-512/256(abc.txt)= 53048e2681941ef99b2e29b76b4c7dabe4c2d0c634fc6d46e0e2f13107e7af23
SHA2-256(stdin)= 881028e5694aabf88235b0f2533d010a39539569333717f1896a8dd949f1d32a
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad  abc.txt

printf, not echo: echo adds a newline, and the digest of "abc\n" is a different value, which is the most common cause of "the hashes don't match" in incident notes.

Which one to choose

  • Default: SHA-256. Universally supported, hardware-accelerated on modern CPUs, required by most standards.
  • Need resistance to length extension with a plain hash: SHA-512/256 or SHA3-256 (or use HMAC).
  • High-speed hashing of large data in software: BLAKE2b or BLAKE3.
  • Variable-length output (key derivation, deterministic randomness): SHAKE256, or HKDF.
  • Post-quantum: none of these needs replacing; 256-bit hashes keep about 128 bits of security against quantum search.

How hash problems are detected

  • Inventory: which hashes appear in certificates (openssl x509 -noout -text | grep Signature), code signing, package lockfiles (sha512- in package-lock.json, --hash=sha256: in pip requirements), backups and file-integrity tools.
  • Mismatched digests in incidents: check input encoding first (trailing newline, CRLF, UTF-8 BOM) before suspecting tampering.
  • Wrong tool: hash(), CRC32 or MurmurHash used where an attacker controls input.

How it is fixed

  • Standardise on SHA-256 (or SHA-512/256, SHA-3, BLAKE2) and remove MD5 and SHA-1 from anything an attacker can influence.
  • Hash bytes, never "strings": define the encoding (UTF-8, no trailing newline) in the protocol.
  • When a secret is involved, use HMAC or keyed BLAKE2, never hash(secret + data).

Pitfalls

  • Comparing a hex digest from one tool with a Base64 digest from another (sha512-… in npm lockfiles is Base64).
  • Hashing data that is ambiguous when concatenated: H("ab" + "c") equals H("a" + "bc"). Encode lengths or use a structured format.
  • Expecting SHA-3 to be faster: in software it is usually slower than SHA-256 with SHA-NI.

Practice

1. A colleague computes echo "abc" | sha256sum and gets a different digest from the vendor's published one for the string abc. Why?

echo appends a newline, so they hashed abc\n. Use printf 'abc' or echo -n.

2. In the lab, choose the fourth message (a payment instruction) and sweep the bit. What range of changed-bit counts do you see, and what would be suspicious?

Values scattered around 128, mostly within about 112–144. A cluster far from 128, or a pattern tied to the bit position, would be suspicious for a cryptographic hash.

3. Which of SHA-256, SHA3-256 and BLAKE2b can be used as a MAC directly by mixing in a key, and how?

BLAKE2b, with its key= parameter. SHA3-256 has the related KMAC construction (and resists length extension, so SHA3(key || msg) is not broken the way SHA-256 is, but KMAC is the standard way). SHA-256 needs HMAC.

Test yourself

About how many of SHA-256's 256 output bits change when one input bit flips?

  1. 1
  2. About 128
  3. All 256
  4. About 16

What is the main structural difference between SHA-3 and SHA-2?

  1. SHA-3 uses a key
  2. SHA-3 is a sponge construction (Keccak), SHA-2 is Merkle-Damgård
  3. SHA-3 has a shorter output
  4. SHA-3 is SHA-2 with more rounds

Which of these is vulnerable to length extension?

  1. SHA-256
  2. SHA3-256
  3. BLAKE2b
  4. SHA-512/256

Why does echo abc | sha256sum not match the published SHA-256 of 'abc'?

  1. sha256sum is a different algorithm
  2. echo adds a trailing newline to the input
  3. The published value is SHA-3
  4. It uses a salt