Please enable JavaScript to use CodeHS

Technology Tutorial

What is Blockchain?

Bitcoin, Dogecoin, NFTs: they all run on blockchain. Learn what a blockchain really is, how hashes lock blocks together, and why cheating the chain is nearly impossible. Then try to tamper with one yourself.

technology ~15 min Beginner friendly
The Big Idea

A shared record book

You have probably heard of Bitcoin, Dogecoin, and NFTs. All of them are built on top of blockchain technology. But blockchain itself is not a kind of money. It is something simpler and more powerful: a way to keep records that everyone can trust.

At its heart, a blockchain is a ledger. A ledger is just a record book of transactions, like the history of a bank account. You start with some money, you add some, you spend some, and the ledger tracks every step so you always know your balance.

A simple ledger
What happenedChangeBalance
Starting Bitcoin+1.001.00
Bought movie tickets-0.100.90
Bought pizza-0.050.85
Bitcoin left0.85

Here is the twist. A normal bank keeps one official copy of the ledger, and you have to trust the bank. A blockchain is a decentralized ledger: there is no bank in charge. Instead, thousands of computers around the world each keep a full copy of the record book, and they work together to verify every new transaction.

That raises a big question. If nobody is in charge, what stops someone from sneaking in and changing an old record? For example, what stops the movie theater from editing its entry to say you sent it more Bitcoin than you really did? The answer is a clever tool called a hash.


The Key Ingredient

What is a hash?

A hash is like a fingerprint for data. A hash function takes any input, like a sentence or a whole file, and turns it into a short scrambled code. Real blockchains use a hash function called SHA-256, which stands for Secure Hash Algorithm 256. Every hash it makes is 256 bits long.

Three rules make hashes so useful:

  • Same input, same output. Hashing "hello" today, tomorrow, or on any computer always gives the exact same code.
  • Tiny change, totally different output. Change one letter, even just "hello" to "Hello", and the whole hash changes beyond recognition.
  • One-way only. You can turn data into a hash, but you cannot turn a hash back into the data. It is a fingerprint, not a container.

Why "fingerprint"? Your fingerprint identifies you, but nobody could rebuild you from just a fingerprint. A hash identifies data the same way: perfect for checking that data has not changed.

Interactive

Hash playground

Type anything below and watch its hash appear instantly. Then change just one letter and compare the new hash with the previous one. Notice how the whole fingerprint changes, not just one part.

Live hash generator

This uses a small teaching hash (8 hex characters) as a simplified stand-in for SHA-256, which is 64 hex characters long.

Previous hash
–
Current hash

Try changing hello to Hello. One capital letter, and every part of the hash scrambles.

Test rule one: delete your text, then type the exact same thing again. You get the exact same hash back. Same input, same output, every time.


Building Blocks

Inside a block

Now for the "block" part of blockchain. A block is one page of the record book. Each block stores three key things:

Data

The transaction being recorded, like "Maria pays Sam 5 coins", plus a timestamp of when it happened.

Previous hash

The fingerprint of the block that came before it. This is the "chain" that links every block to its neighbor.

Its own hash

A fingerprint computed from everything inside the block, including the previous hash and the data.

Here is the trick that makes blockchain tamper-proof. Each block's hash is computed from its data and the previous block's hash. So the blocks are chained together by their fingerprints:

  • If you change the data in a block, that block's hash changes.
  • The next block still stores the old hash as its "previous hash", so the link no longer matches.
  • That mismatch breaks the chain, and every block after it fails verification.

Change one old record and the whole chain lights up with evidence. Do not just take our word for it. Try it yourself below.

Interactive

Try to tamper with a chain

Here is a tiny blockchain with three transactions. Pretend you are Sam, and you want to cheat by editing Block 2 to say Maria paid you 50 coins instead of 5. Go ahead, edit the data and watch what happens to the chain.

Tamper lab

Edit the data in any block. Hashes recompute live using the same teaching hash from the playground above.

Block 1 Verified
Previous hash0
This block's hash
Block 2 Verified
Previous hash
This block's hash
Block 3 Verified
Previous hash
This block's hash
The chain is valid. Every "previous hash" matches its neighbor's fingerprint.

Why cheaters lose: you just re-mined 3 blocks with one click. On a real network, your edited copy would disagree with thousands of other copies of the ledger, and the network sides with the majority. To actually cheat, you would have to re-mine the chain on most of those computers at the same time. That is practically impossible, and that is the whole point.


A Peek Behind the Scenes

Build a tiny blockchain in code

Real blockchain code has the same three jobs you just saw: hash data, link blocks, and verify the chain. The original version of this tutorial builds it in Java with SHA-256. Here is a simplified Python version that uses our small teaching hash instead, so the fingerprints match the demos on this page. Press Run:

tiny_blockchain.py
# A teaching hash: a simple stand-in for SHA-256
def tiny_hash(text):
    value = 2166136261
    for ch in text:
        value = (value ^ ord(ch)) * 16777619 % 2**32
    return format(value, "08x")

class Block:
    def __init__(self, data, previous_hash):
        self.data = data
        self.previous_hash = previous_hash
        # The block's fingerprint covers the previous
        # hash AND the data, chaining the blocks together
        self.hash = tiny_hash(previous_hash + data)

# Build the chain, block by block
chain = []
chain.append(Block("Maria pays Sam 5 coins", "0"))
chain.append(Block("Sam pays Ana 2 coins", chain[0].hash))
chain.append(Block("Ana pays Leo 1 coin", chain[1].hash))

def is_chain_valid():
    # Every block must point at its neighbor's real hash
    for i in range(1, len(chain)):
        if chain[i].previous_hash != chain[i - 1].hash:
            return False
    return True

for i, block in enumerate(chain):
    print("Block", i + 1, "hash:", block.hash)
print("Is the chain valid?", is_chain_valid())
Console
Block 1 hash: 5d980494
Block 2 hash: fb796088
Block 3 hash: b0b1fc0e
Is the chain valid? True

Look familiar? Those are the exact hashes from the hero image and the tamper lab. Same input, same output. Real blockchain code also mixes a timestamp and a nonce (a "number used once") into each hash, but the chaining idea is exactly what you see here.


Strength in Numbers

Thousands of copies, one truth

Chained hashes make tampering visible. Decentralization makes it useless. Because thousands of computers each hold a full copy of the blockchain, everyone can check everyone else's work:

  • When a new block is added, computers on the network verify it and add it to their own copy.
  • If one copy has been tampered with, its hashes will not match the others.
  • The network trusts the version that the majority of computers agree on. This agreement is called consensus.

On networks like Bitcoin, adding a block takes real work. To mine a block, a computer must find a hash that meets a difficulty target, like starting with a certain number of zeros. The only way to find one is brute force: change the nonce, hash again, and repeat millions of times. Higher difficulty means many more tries.

How hard is it really? Bitcoin miners use extremely powerful computers, and it still takes the network about 20 minutes on average to mine a coin. The difficulty value they are working against is over 17 trillion. Rewriting old blocks on thousands of computers at once is simply not realistic.

Real World

Where blockchain is used

Cryptocurrency made blockchain famous, but the idea of a tamper-proof shared record book is useful anywhere people need to trust records without trusting a single record-keeper:

Cryptocurrency

Bitcoin, Dogecoin, and others use a blockchain as their entire ledger of who owns what.

NFTs

Non-fungible tokens use a blockchain to record who owns a unique digital item.

Supply chains

Companies track products from factory to store, with a digital trail nobody can quietly edit.

Records and contracts

Medical records, property deeds, and digital contracts can be verified without a middleman.


Code

A tiny blockchain in Python

A blockchain links blocks together by storing a hash of the previous block. This Python program builds a three-block chain and verifies it.


Check Yourself

Quick quiz

1. What happens to a hash if you change one letter of the input?

2. Why does editing the data in Block 2 break Block 3?

3. What makes a blockchain "decentralized"?

Keep exploring the chain

You now know the secret behind blockchain: hashes chained together, copied across thousands of computers. Next, see what people build on top of it, or dig deeper into the cryptography that keeps data safe.