DAY 22 / 40
>> Module 03 / Day 22 / Lecture

Hashing

The magic fingerprint behind every block

Yesterday we said "change one character and the chain breaks." Today you'll see exactly why — and write the Python to prove it.

Lecture 75 min
Lab 60 min  ·  Hash strings, observe avalanche
Module M3  ·  Blockchain Basics
DAY 22 / 40
>> Today's roadmap

From data to fingerprint

  1. What is a hash function? — any input, fixed output
  2. The four properties that make it powerful
  3. SHA-256 in Python — just two lines of code
  4. The avalanche effect — live demo
  5. Lab — hash strings and see the chain connection
DAY 22 / 40
>> 01 / 04 · The idea

A hash turns anything into a fixed-size fingerprint

Feed a hash function any input — a word, a sentence, an entire book. It always produces an output of exactly the same length. Change even one character and the output is completely different.

hash_idea.py
import hashlib def sha256(text): return hashlib.sha256(text.encode()).hexdigest() print(sha256("hello")) # → 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 print(sha256("Hello")) # Capital H # → 185f8db32921bd46d35cc88b7422e0fcedb2b3f0f7b1460c4b02b2cdc45a6f8
Only one capital letter changed — and the output is completely different. This is not a coincidence. It is by design.
DAY 22 / 40
>> 02 / 04 · The properties

Four rules every hash function follows

DETERMINISTIC

Same input always gives same output. Hash("hello") is always the same value, every time, on every computer.

FIXED LENGTH

No matter how long the input, the output is always 64 hex characters (256 bits for SHA-256).

ONE-WAY

You can go from data → hash instantly. You cannot go hash → data. It's a one-way door.

AVALANCHE EFFECT

Change one bit of input and roughly half the output bits flip. Small change = completely different fingerprint.

DAY 22 / 40
>> 03 / 04 · Python code

SHA-256 in two lines

sha256.py
import hashlib text = "Polkadot block #21000000" hash_value = hashlib.sha256(text.encode()).hexdigest() print(hash_value) # → some 64-character hex string # Step by step: # text.encode() → converts string to bytes # hashlib.sha256(...) → creates SHA-256 hash object # .hexdigest() → returns result as hex string
64 CHARACTERS

Always exactly 64 hex chars = 256 bits. Every block on every blockchain has one of these.

HEXADECIMAL

The output uses 0-9 and a-f. You saw hex in Day 2 of the old syllabus — it's just base-16.

DAY 22 / 40
>> 04 / 04 · The key property

One character. Completely different hash.

avalanche.py
import hashlib def h(t): return hashlib.sha256(t.encode()).hexdigest() print(h("blockchain")) # 5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9 print(h("Blockchain")) # capital B # ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f print(h("blockchain!")) # added ! # 9c827201a94b6c9e3b2aaa7e76f55e3ead3f0e8d2e67c3a25b0a1c07af6af3e1
Why this matters for the chain: If you change anything inside a block, its hash changes. The next block stores the OLD hash — so it no longer matches. The chain is broken. You can't secretly edit history.
DAY 22 / 40
>> Connecting to the chain

Each block seals all previous history

Block 1
hash: a3f7...
data: genesis
→
Block 2
hash: 9c82...
prev: a3f7...
data: Alice→Bob: 10
→
Block 3
hash: ef79...
prev: 9c82...
data: Bob→Carol: 4
Tamper with Block 2? Its hash changes. Block 3's prev_hash now points to the old, wrong hash. The chain is invalid — and anyone can see it.
DAY 22 / 40
>> 11:00 – 12:00 · Lab

Today's lab — Hashing Explorer

CORE "Avalanche explorer"

Hash 5 different strings. Then for each one, change a single character and hash again. Print both side by side and note how different they are.

★ STRETCH "Hash a Python dict"

Convert a dict to a string (use str()), hash it. Change one value in the dict, hash again. Prove the hash is different — this is exactly how blocks work.

How to submit. Create students/<username>/D22_hashing.py. PR before 12:00.
DAY 22 / 40
>> End of Day 22

Your new superpower

  • ✓ A hash is a fixed-size fingerprint of any data — made with hashlib.sha256().
  • ✓ The avalanche effect: change one character, get a completely different hash.
  • ✓ This is why blockchains are immutable: any change breaks the chain of hashes.
01 / 9
←/→ navigate   F fullscreen