Overview
SHA-256 (Secure Hash Algorithm 256-bit) is the cryptographic hash function that underpins Bitcoin's proof-of-work mining, transaction identification, and many other protocol operations. Designed by the NSA and published by NIST in 2001, SHA-256 takes an input of any size and produces a fixed 256-bit (32-byte) output called a digest or hash. It is a one-way function: given a hash, it is computationally infeasible to determine the original input.
Properties of SHA-256
SHA-256 Properties:
Input: "Hello"
Output: 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969
Input: "hello" (different case)
Output: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
Key Properties:
┌─────────────────────────────────────────────────┐
│ 1. Deterministic Same input always = same │
│ output │
│ 2. Fast Efficient to compute │
│ 3. Avalanche effect Tiny input change = │
│ completely different output │
│ 4. Pre-image Cannot reverse the hash │
│ resistant to find the input │
│ 5. Collision Infeasible to find two │
│ resistant inputs with same hash │
│ 6. Fixed size Always 256 bits (32 bytes) │
└─────────────────────────────────────────────────┘
SHA-256 in Bitcoin
Bitcoin uses SHA-256 in multiple critical operations:
- Mining — Block headers are hashed with double SHA-256 (SHA-256 applied twice). Miners search for a nonce that produces a hash below the difficulty target.
- Transaction IDs — Each transaction is identified by the double SHA-256 hash of its serialized data.
- Merkle trees — Transactions within a block are organized into a Merkle tree using double SHA-256, producing a single Merkle root stored in the block header.
- Address generation — SHA-256 is used as part of the HASH160 operation (SHA-256 followed by RIPEMD-160) to derive addresses from public keys.
Double SHA-256
Bitcoin uses SHA-256 twice in succession (often written as SHA-256d or HASH256) for mining and transaction IDs. This double hashing provides protection against certain theoretical attacks, including length-extension attacks, where an attacker could append data to a message and compute a valid hash without knowing the original message.
Double SHA-256:
Input Data
│
▼
┌────────┐ ┌────────┐
│SHA-256 │────>│SHA-256 │────> Final Hash
│ Pass 1 │ │ Pass 2 │ (used for mining,
└────────┘ └────────┘ TXIDs, etc.)
Mining and SHA-256
Bitcoin mining is essentially a massive, parallel SHA-256 computation. Miners hash block headers billions of times per second, incrementing the nonce and other fields, searching for a hash that starts with enough leading zeros to satisfy the current difficulty target. The global Bitcoin network collectively performs on the order of hundreds of exahashes (10^18 hashes) per second.
Common Misconception
SHA-256 is sometimes confused with encryption, but hashing and encryption are fundamentally different. Encryption is reversible with a key (you can decrypt ciphertext back to plaintext). SHA-256 is a one-way function — there is no key and no way to "unhash" the output back to the input. SHA-256 produces a fingerprint of data, not a secret message.