Cryptography

Hash functions

A hash function takes any data — a word, a book, an entire hard drive — and turns it into a short, fixed-length fingerprint. It’s the workhorse of Bitcoin: it links the blockchain together, powers mining, and lets a global network verify data without trusting anyone.

Try it — hash anything, live

Type anything below and its fingerprint appears instantly. It defaults to SHA-256 — the exact 256-bit hash the Bitcoin network computes billions of times a second — but you can switch to RIPEMD-160, to the combined RIPEMD-160(SHA-256()) that builds addresses (nicknamed HASH160), or to All to compare the three side by side. We’ll see why Bitcoin uses more than one just below.

Four properties that make it magic

  • Deterministic. The same input always gives the same hash — every computer on Earth agrees. Type “Bitcoin” again and you’ll get exactly the fingerprint above.
  • Avalanche. Change a single character — even the capital B — and the hash changes completely, with no resemblance to the old one. Try it: the fingerprint scrambles entirely.
  • Fixed length. One word or a whole encyclopedia, the output is always 256 bits (64 hex characters). A fingerprint is always the same size, whatever it summarises.
  • One-way. Easy to compute the hash from the data; effectively impossible to work backwards from a hash to the data. It’s a one-way street — trivial forwards, hopeless in reverse (and unlike a trapdoor, there’s no secret that lets anyone undo it).

Because a hash uniquely fingerprints its input, you can prove two files are identical by comparing 64 characters instead of the whole files — exactly what BitTorrent does for each piece. And because it’s one-way, you can commit to a value publicly without revealing it. Bitcoin leans on both tricks constantly.

Where Bitcoin uses it

Each block of transactions is stamped with the hash of the block before it, so the blocks form an unbreakable chain — change any old transaction and every later hash breaks, and the whole network notices. Mining, as we’ll see, is a race to find an input whose hash starts with a run of zeros. Hashing is the glue and the engine both.

Why two different hash functions?

SHA-256 is the network’s workhorse — it secures mining and proof-of-work, chains the blocks together, and fingerprints every transaction. But there’s a second hash, RIPEMD-160, used in one specific place: your address, which is built as RIPEMD-160(SHA-256(public key)) — a combination nicknamed HASH160. Why hash twice, with two different algorithms?

  • Size. RIPEMD-160’s output is 160 bits (20 bytes) instead of 256, making addresses shorter and cheaper to store and type — while still being astronomically hard to collide (you’d need to try roughly 2⁸⁰ inputs).
  • Diversity. The two functions come from unrelated designs, so even if a weakness were ever found in one, the other still stands guard. Layering them is belt-and-suspenders security.

Switch the demo above to RIPEMD-160 and you can watch its shorter, 40-character fingerprint for yourself — then see the full public-key-to-address pipeline in action in the Addresses lesson.

Hashing proves data hasn’t changed. But the moment you hash something like a password, its perfect consistency becomes a weakness — and the fix is a pinch of salt, next.

Key takeaways

  • A hash function turns any data into a fixed-length fingerprint — deterministic, one-way, with a total avalanche when the input changes.
  • Bitcoin uses SHA-256 to link blocks, power mining, and let anyone verify data without trusting the source.

Check yourself

Which is NOT a property of a good hash function?

The ‘avalanche’ property of a hash means:

Spotted an error or have feedback on this lesson? Suggest a correction ↗

Comments

Powered by Nostr — reply from any Nostr client, and zap the lesson over Lightning.