Powered by curiosity

Web3 Hash Lab

A sandbox for building an intuitive understanding of blockchains, step by step.

SHA256 Hash

SHA-256 is a one-way cryptographic hash that turns any input into a fixed 256-bit fingerprint. Even tiny input changes produce a very different hash.

Data:

Bits:

Hex as 0-15:

Hex:

Hash:

64 hex symbols x 4 bits per symbol = 256 bits (32 bytes x 8 bits).

Block

The difficulty target sets the rule a hash must satisfy to count as mined (valid).
In this demo, a valid block hash must start with 0000.

Block:

Block number is this block's position in the blockchain.

Nonce:

Nonce is the counter miners change to search for a valid hash.

Data:

Hash:

Mined with prefix 0000.

Blockchain

A blockchain links each block to the one before it by storing the previous block's hash in the payload. If an earlier block changes, every block after it must be mined again.

Block:

Nonce:

Data:

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Data:

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Data:

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Data:

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Data:

Prev:

Hash:

Mined with prefix 0000.

Distributed Blockchain

Each peer keeps its own blockchain copy.
If chain was altered, its tip hash will be different from original chain.

Peer A

Tip hash

Reference chain

0000bb2fb1a5d6d03b4a1fe43bd97f98a037953315b287e07e7269b9e640818f

Peer B

Tip hash

Synced with Peer A

0000bb2fb1a5d6d03b4a1fe43bd97f98a037953315b287e07e7269b9e640818f

Peer C

Tip hash

Different chain

0000859218d50abd9235743a27b72f52c541ebcf3706ee35dc18678c8266a6d4

Peer A

Block:

Nonce:

Data:

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Data:

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Data:

Prev:

Hash:

Mined with prefix 0000.

Peer B

Block:

Nonce:

Data:

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Data:

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Data:

Prev:

Hash:

Mined with prefix 0000.

Peer C

Block:

Nonce:

Data:

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Data:

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Data:

Prev:

Hash:

Mined with prefix 0000.

Tokens

A token represents a transfer of digital value on the blockchain. Each token transactions contain three fields:
• amount — how much value is being transferred
• from — the sender
• to — the recipient

Balance = total received - total sent.

Anonymity ~ public ids are stored, not real names.

Peer A

Tip hash

Reference chain

00003e5f9d25ae04c9750e8d836142e3e13f744d8ecfd9440046887f56dc931c

Balances

Anders$300.00
Captain-$2,970.96
Ilsa$1,019.49
Jan$364.95
Rick-$483.51
Sam$295.83
Strasser$2,606.01
Victor-$831.81

Peer B

Tip hash

Synced with Peer A

00003e5f9d25ae04c9750e8d836142e3e13f744d8ecfd9440046887f56dc931c

Balances

Anders$300.00
Captain-$2,970.96
Ilsa$1,019.49
Jan$364.95
Rick-$483.51
Sam$295.83
Strasser$2,606.01
Victor-$831.81

Peer C

Tip hash

Different chain

0000f03fbf335541da26a3fad3ea09e4d139c6c55d8a200348a67804fef3f303

Balances

Anders$300.00
Captain-$3,009.81
Ilsa$1,041.69
Jan$392.70
Rick-$511.26
Sam$318.03
Strasser$2,617.11
Victor-$848.46

Peer A

Block:

Nonce:

Coinbase:

$->

Coinbase mints new tokens for the miner and is not debited from another wallet.

Tx:

$From:->
$From:->
$From:->
$From:->
$From:->

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Coinbase:

$->

Coinbase mints new tokens for the miner and is not debited from another wallet.

Tx:

$From:->
$From:->
$From:->
$From:->
$From:->

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Coinbase:

$->

Coinbase mints new tokens for the miner and is not debited from another wallet.

Tx:

$From:->
$From:->
$From:->
$From:->
$From:->

Prev:

Hash:

Mined with prefix 0000.

Peer B

Block:

Nonce:

Coinbase:

$->

Coinbase mints new tokens for the miner and is not debited from another wallet.

Tx:

$From:->
$From:->
$From:->
$From:->
$From:->

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Coinbase:

$->

Coinbase mints new tokens for the miner and is not debited from another wallet.

Tx:

$From:->
$From:->
$From:->
$From:->
$From:->

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Coinbase:

$->

Coinbase mints new tokens for the miner and is not debited from another wallet.

Tx:

$From:->
$From:->
$From:->
$From:->
$From:->

Prev:

Hash:

Mined with prefix 0000.

Peer C

Block:

Nonce:

Coinbase:

$->

Coinbase mints new tokens for the miner and is not debited from another wallet.

Tx:

$From:->
$From:->
$From:->
$From:->
$From:->

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Coinbase:

$->

Coinbase mints new tokens for the miner and is not debited from another wallet.

Tx:

$From:->
$From:->
$From:->
$From:->
$From:->

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Coinbase:

$->

Coinbase mints new tokens for the miner and is not debited from another wallet.

Tx:

$From:->
$From:->
$From:->
$From:->
$From:->

Prev:

Hash:

Mined with prefix 0000.

Public / Private Key Pairs

Symmetric encryption demo: Caesar cipher

Data → encrypt with secret key → gibberish.
Gibberish → decrypt with the same secret key → original data.

Data

Secret Key (for Caesar - number by which to shift characters)

Encrypt and decrypt both use the same shared key.

Data

HELLO

Encrypt key

+3

Gibberish

KHOOR

Decrypt key

-3

Original Data

HELLO

Problem - how to share the key with someone?

Meeting in person is not convenient, and sending it is unsafe.

Asymmetric encryption demo

Generate two linked keys and select one of them to be private, and the other to be public.
Linked means you cannot guess one key knowing the other, but anything encrypted with one key can be decrypted with the other.

Data → encrypt with private key → gibberish.
Gibberish → decrypt with the matching public key → original data.

Private Key

Valid secp256k1 private key.

Public Key

Uncompressed format: 04 + X (32 bytes) + Y (32 bytes).

Data

Data

HELLO

Encrypt key

private key (1)

Gibberish

21507 14985 15309 15309 18730

Decrypt key

public key (0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8)

Original Data

HELLO

Me

Private key (I keep it secret from everyone)

1

Public key (shared on email, documents, key server etc.)

0479be667ef9dcbbac55...47d08ffb10d4b8

We share public keys

Other person

Private key

***************** (their secret)

Public key

0457e66226392a1aa161...ed92121ef6a69e

1) Me encrypt with other person public key

Only they know their private key, so only they can decrypt and read the message.

Data

Me

HELLO

other's public key
0457...a69e

Gibberish

Network / transit

27d6ea8ea1

other's private key
********

Decrypted

Other person - reads the message

HELLO

2) Me encrypt with my private key

Everyone knows my public key, so anyone can decrypt and read the message.
Because only I know my private key - only I can encrypt in such a way that it is decrypted with my public key, so everyone knows that I was the one who sent the message.

Data

Me

HELLO

my private key
1...1

Gibberish

Network / transit

25c134c2f9

my public key
0479...d4b8

Decrypted

Other person - knows message is from me

HELLO

3) Both: Me encrypt with my private key, then encrypt with other person public key

Other person decrypts twice in reverse order to recover the original data.

Because I used my private key - the other person can verify the message came from me.
Because I used the other person's public key - only they can decrypt and read the message.

Data

Me

HELLO

my private key
1...1

gibberish (signed payload)

Me

25c134c2f9

other's public key
0457...a69e

Outer gibberish

Network / transit

5da6c5f3dde1883c4874

other's private key
********

Decrypted signed payload

Other person

25c134c2f9

my public key
0479...d4b8

Final decrypted

Other person

HELLO

Math behind key pairs

Why and how key pairs actually work and encrypt or decrypt data?
Explore the math behind key pairs on a dedicated page:

Signed Transaction

A signed transaction message includes amount, from public key, and to public key.

The sender signs this message with their private key.

From Me

My private key

Valid sender private key.

My public key

To Other Person

Their public key

Message

$From:->

Instead of names - public keys of people.

Message Signature

(not signed yet)

Message Signature Decrypted

(verify to decrypt signature)

Signed Blockchain

A signed block stores token transfers where sender and recipient are public keys.

Each transaction contains four fields:

- amount: how much value is being transferred

- from: sender public key

- to: recipient public key

- sig: sender signature over transaction data

Balance = total received - total sent.

Identity is represented by public keys instead of names.

Peer A

Tip hash

Reference chain

000007d4949a00392b336882262354d1ff86cbc172d8f7594effc9ac1cc49cee

Balances

041c377677bb6973$17.63
042222d7af343abd-$5.74
0451d4a9c44a2dec-$15.37
04cc17dc129331c1$10.37
04d4080959e3795b-$8.52
04f062911b31d524-$4.48
04fe1be031bc7a54$306.11

Peer B

Tip hash

Synced with Peer A

000007d4949a00392b336882262354d1ff86cbc172d8f7594effc9ac1cc49cee

Balances

041c377677bb6973$17.63
042222d7af343abd-$5.74
0451d4a9c44a2dec-$15.37
04cc17dc129331c1$10.37
04d4080959e3795b-$8.52
04f062911b31d524-$4.48
04fe1be031bc7a54$306.11

Peer C

Tip hash

Different chain

00000323bb3e6ac68844f498cde36adbc0ecf503e8fee833d7cad64f5b62b45e

Balances

041c377677bb6973$9.81
042222d7af343abd$8.52
0451d4a9c44a2dec$21.33
04cc17dc129331c1-$6.33
04d4080959e3795b-$9.44
04f062911b31d524-$17.22
04fe1be031bc7a54$293.33

Peer A

Block:

Nonce:

Coinbase:

$->

Coinbase mints new tokens for the miner and is not debited from another wallet.

Tx:

$From:->
Sig:
$From:->
Sig:
$From:->
Sig:

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Coinbase:

$->

Coinbase mints new tokens for the miner and is not debited from another wallet.

Tx:

$From:->
Sig:
$From:->
Sig:
$From:->
Sig:

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Coinbase:

$->

Coinbase mints new tokens for the miner and is not debited from another wallet.

Tx:

$From:->
Sig:
$From:->
Sig:
$From:->
Sig:

Prev:

Hash:

Mined with prefix 0000.

Peer B

Block:

Nonce:

Coinbase:

$->

Coinbase mints new tokens for the miner and is not debited from another wallet.

Tx:

$From:->
Sig:
$From:->
Sig:
$From:->
Sig:

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Coinbase:

$->

Coinbase mints new tokens for the miner and is not debited from another wallet.

Tx:

$From:->
Sig:
$From:->
Sig:
$From:->
Sig:

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Coinbase:

$->

Coinbase mints new tokens for the miner and is not debited from another wallet.

Tx:

$From:->
Sig:
$From:->
Sig:
$From:->
Sig:

Prev:

Hash:

Mined with prefix 0000.

Peer C

Block:

Nonce:

Coinbase:

$->

Coinbase mints new tokens for the miner and is not debited from another wallet.

Tx:

$From:->
Sig:
$From:->
Sig:
$From:->
Sig:

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Coinbase:

$->

Coinbase mints new tokens for the miner and is not debited from another wallet.

Tx:

$From:->
Sig:
$From:->
Sig:
$From:->
Sig:

Prev:

Hash:

Mined with prefix 0000.

Block:

Nonce:

Coinbase:

$->

Coinbase mints new tokens for the miner and is not debited from another wallet.

Tx:

$From:->
Sig:
$From:->
Sig:
$From:->
Sig:

Prev:

Hash:

Mined with prefix 0000.