Learning LibraryBlockchain & DeFi LibraryTeens

Hashing: a fingerprint for any data

A hash is a fingerprint for data. Feed in anything — a word, a whole book — and you get a fixed-length code back. Change a single letter, and the fingerprint changes completely.

The big idea

A hash function turns any input into a fixed-size digest; the same input always gives the same hash, and any change gives a totally different one.

See it in code

1The basics

At its core, a hash turns any input into a fixed-size fingerprint. Feed one block's contents to SHA-256 and you get back a 64-character code:

python
import hashlib

def sha(text):
    return hashlib.sha256(text.encode()).hexdigest()

print(sha("Alice pays Bob"))
Run it — any text in, one 64-character fingerprint out:
c4d09eac2af127257bc9a3c2f2e527c4115373919d532d8ab9f336490495aa24

That 64-character code is the digest. It's the same length whether you hash three words or three whole books — that's the 'fixed-size' part.

2A step further

A hash is also deterministic: the exact same input always produces the exact same fingerprint. Hash the same block twice and the two digests are identical, character for character:

python
import hashlib

def sha(text):
    return hashlib.sha256(text.encode()).hexdigest()

print(sha("Alice pays Bob"))
print(sha("Alice pays Bob"))
Run it — same input, byte-for-byte the same hash:
c4d09eac2af127257bc9a3c2f2e527c4115373919d532d8ab9f336490495aa24
c4d09eac2af127257bc9a3c2f2e527c4115373919d532d8ab9f336490495aa24

Identical every time — which is exactly why hashing lets you verify data: re-hash it and compare. But watch what happens when the input changes by a single letter.

3In our world

Now change just one letter — Bob to Bib — and the entire 64-character fingerprint comes back unrecognizably different:

python
import hashlib

def sha(text):
    return hashlib.sha256(text.encode()).hexdigest()

print(sha("Block 1: Alice pays Bob"))
print(sha("Block 1: Alice pays Bib"))
Run it — one changed letter, an entirely different hash:
9dcff5017f4b2ab617dceef999fe77b11e2114186eaec7be63ce0a4cdcac581b
b1e71c9ec721c1c8c4596475bbfd60755d98f18cb38be9df604fd62a1ae1d0bf

Both hashes are exactly 64 hex characters, no matter how long the input is — that's the 'fixed-size' part. And the tiny Bob-to-Bib change scrambled everything (the 'avalanche effect'). Crucially, hashing is one-way: you can't run it backward to recover the input.

The same idea, everywhere

Hashing is a workhorse of computing far beyond crypto. It stores passwords safely (save the hash, never the password), checks that a downloaded file wasn't corrupted, and powers fast lookups in dictionaries. Any place you need a compact, change-sensitive fingerprint, a hash does the job.

Try it yourself

Hash your own name, then change one letter and compare. Then hash a huge string and a tiny one — notice both digests are the same length. Try hashlib.md5 versus sha256 to see different fingerprint sizes.

The common mistake

Thinking a hash is encryption you can reverse. It isn't — there's no 'unhash'. That one-way property is the point: you verify by re-hashing and comparing, never by decoding. (And for passwords, real systems add a 'salt' — a topic for later.)

What it unlocks

Hashing is the foundation of blocks and chains, proof of work, and what a blockchain is — all built on string methods.