2 min to read.
SHA-256
A cryptographic hash function that turns any input into a fixed 256-bit output; the hash function used by Bitcoin.
What is SHA-256?
SHA-256 is a cryptographic hash function. It takes an input of any size and produces a fixed-length output of 256 bits, usually written as 64 hexadecimal characters. It belongs to the SHA-2 family, which was designed by the US National Security Agency and published as a standard by NIST. Bitcoin relies on SHA-256 throughout its design.
How it works
A hash function is a one-way calculation. SHA-256 has the properties that make such a function useful for security:
- Deterministic: the same input always gives the same output.
- Fixed size: a single letter and a whole library both produce 256 bits.
- One-way: it is not feasible to work back from an output to the input.
- Avalanche effect: changing one character of the input produces a completely different output.
- Collision resistance: no practical way is known to find two different inputs with the same output.
A hash is often described as a digital fingerprint of data.
Example
Hashing the word "bitcoin" gives one 64-character string. Hashing "Bitcoin", with a capital B, gives another string that has no visible resemblance to the first. Anyone can repeat the calculation and will get the same results.
How Bitcoin uses it
- Mining. Miners hash a block header with SHA-256 twice, over and over with different values, searching for a result below the network's target. This is the proof of work.
- Linking blocks. Each block header contains the hash of the previous header, so changing an old block would change every hash after it.
- Transactions. Transaction IDs are hashes, and the transactions in a block are summarised in a Merkle tree of hashes.
- Addresses. Hashing, with SHA-256 as one of the steps, is part of deriving an address from a public key.
Why it matters
The security of Bitcoin's ledger depends on SHA-256 behaving as designed. No practical attack that breaks it is publicly known.
Common misunderstandings
Hashing is not encryption: there is no key and nothing to decrypt. Ethereum mainly uses a different hash function, Keccak-256.