Cryptographic Hash Functions: SHA-256, Properties, Avalanche Effect and a Python Hands-On Demo

Blockchain Developer 50 min min read Updated: Nov 17, 2026 Beginner
Cryptographic Hash Functions: SHA-256, Properties, Avalanche Effect and a Python Hands-On Demo
Beginner Topic 1 of 3

What Is a Hash Function?

A hash function takes any input (text, a file, a block) and produces a fixed-size output called a hash or digest. SHA-256, used by Bitcoin, always gives 256 bits (64 hexadecimal characters).

Important Properties

PropertyMeaningWhy blockchain needs it
DeterministicSame input always gives the same hashEveryone can verify
Fast to computeEasy to hash any dataPractical for nodes
One-wayCannot get the input back from the hashHides the data, protects keys
Avalanche effectTiny change gives a totally different hashDetects tampering
Collision resistantPractically impossible to find two inputs with the same hashHash is a unique fingerprint

Try It in Python

python import hashlib def sha256(text): return hashlib.sha256(text.encode()).hexdigest() print(sha256("hello")) print(sha256("hello")) # same input, same hash print(sha256("Hello")) # one letter changed, totally different print(len(sha256("a very long sentence " * 1000))) # always 64
Result

2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969 64

A Tiny Hash Chain

python import hashlib def h(text): return hashlib.sha256(text.encode()).hexdigest() blocks = [] prev = "0" * 64 for data in ["Asha pays Ravi 5", "Ravi pays Meena 2", "Meena pays Asha 1"]: block_hash = h(prev + data) blocks.append({"data": data, "prev": prev, "hash": block_hash}) prev = block_hash def is_valid(chain): prev = "0" * 64 for b in chain: if b["prev"] != prev or h(prev + b["data"]) != b["hash"]: return False prev = b["hash"] return True print("valid:", is_valid(blocks)) blocks[1]["data"] = "Ravi pays Meena 200" # tamper print("after tamper:", is_valid(blocks))
Result

valid: True after tamper: False

Changing block 2's data breaks its hash, so the chain check fails. This is exactly how real blockchains detect tampering.

Where Blockchains Use Hashes

  • Linking blocks (previous hash)
  • Merkle trees
  • Creating addresses from public keys
  • Proof of work puzzles
  • Transaction IDs
Not encryption Hashing is not encryption. Encryption can be reversed with a key. A hash cannot be reversed.

Practice

  1. Hash your name and then your name with one extra letter. Compare
  2. Add a fourth block to the demo chain
  3. Why is it a problem if two inputs gave the same hash?

Conclusion

A hash is a fixed-size fingerprint. SHA-256 is deterministic, one-way and collision resistant, with an avalanche effect that makes tampering easy to detect.

Quick Summary: meaning; properties; Python demo; hash chain; uses.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators