Blockchain/Level II · Mechanics/2.1 Cryptography/Lesson 1
Hash functions and SHA-256
The fingerprint that anchors the ledger
MaterialsA terminal with shasum or openssl installedAn online SHA-256 calculatorTwo text files differing by a single character
Wonder
- Why does changing one character in a file alter half its hash?
- If endless possible inputs share a finite set of outputs, why is it still infeasible to find two that collide?
- What made SHA-256 safe when its predecessor, SHA-1, was broken?
Explore
- A cryptographic hash function maps arbitrary-length data to a fixed-size digest. It is deterministic: the same input always yields the identical output. It is fast to compute but infeasible to reverse. This one-way property defines preimage resistance. If an attacker holds only the hash, they cannot derive the original message. This irreversibility secures integrity checks and identity proofs across distributed systems.
- SHA-256 produces a 256-bit output regardless of input size. Whether the input is a single byte or a gigabyte file, the digest remains exactly 32 bytes. This fixed length allows consistent storage and comparison. The function processes data in 512-bit blocks through 64 rounds of logical operations. Its design follows the Merkle–Damgård construction, chaining a compression function across the blocks; the compression function’s mixing is what scrambles the output.
- The avalanche effect is a critical feature: flipping one bit in the input changes approximately half the bits in the output hash. This sensitivity ensures that even minor data corruption or tampering results in a completely different digest. It makes hash functions ideal for verifying file integrity and detecting alterations. Any modification, no matter how subtle, breaks the match between the expected and computed hashes.
- Collision resistance is the most demanding property. Finding two distinct inputs that produce the same hash requires roughly 2^128 operations because of the birthday bound. For SHA-256, this computational cost renders brute-force attacks impractical with current hardware. In contrast, SHA-1 suffered from a practical collision attack demonstrated by researchers at Google and CWI in 2017, known as SHAttered. This vulnerability forced migration away from SHA-1 for security-critical applications.
- Bitcoin applies SHA-256 twice, a practice called double hashing. Block headers and transaction IDs are hashed with SHA-256, then the result is hashed again with SHA-256. Satoshi never explained the choice; a common explanation is protection against length-extension attacks, a known property of Merkle–Damgård hashes such as SHA-256. Every block hash and transaction ID on the chain is the output of this double pass.
- Open any terminal or online tool and compute the SHA-256 hash of the string ‘MSTR Cat’. Then compute the hash of ‘MSTR cat’, changing only the capitalization of one letter. Compare the two 64-character hexadecimal outputs. They will be entirely different, sharing no recognizable pattern. This immediate divergence illustrates the avalanche effect in action: a single bit change scrambles the entire digest, confirming that the function treats inputs with extreme sensitivity.
Key facts
- SHA-256 produces a fixed 256-bit output for any input size.
- The avalanche effect ensures one bit change alters about half the hash bits.
- Finding a SHA-256 collision by brute force takes about 2^128 operations.
- SHA-1 was broken by a practical collision attack (SHAttered) in 2017.
- Bitcoin uses double SHA-256 for block hashes and transaction IDs.
- SHA-256 processes data in 512-bit blocks through 64 rounds.
Exercise nine, six, three, one
9CollectList 9 hash functions or checksum algorithms used in computing. Note their output sizes and primary use cases.
6ClusterGroup the 9 into 6 categories based on security strength: broken, deprecated, standard for general use, standard for high-security contexts, specialized for speed, or other.
3DistillReduce the 6 groups to 3 core reasons why a hash function is selected for a specific application.
1CrystallizeWrite 1 sentence explaining how fixed-length digests enable verification of variable-sized data.
Fact check
Dates, figures and claims checked against the published sources listed below.