Tutorial Information
Required Tools
- Python 3
- Python cryptography library
- Browser wallet (e.g., MetaMask)
Materials
- Laptop or desktop with internet access
- Modern web browser (Chrome, Firefox, or Brave)
Steps
- Set up your tools: Install Python and the cryptography library, and make sure you have a browser wallet like MetaMask ready.
- Try a simple hash: Use Python to hash a message so you can see how small changes create completely different outputs.
- Generate a key pair: Create a public/private key pair in Python and connect the concept to the keys your wallet manages.
- Sign and verify a message: Sign a short message with your private key and verify it with your public key to see how digital signatures work.
- Apply this to your wallet and daily security: Map the concepts back to real wallets, transactions, and simple safety habits you can keep using.
What you’ll learn and what you need
In this tutorial you’ll walk through the same steps I use when I explain “how crypto actually works” to new engineers and friends:
- See how a hash function makes even a tiny change in data obvious.
- Generate a public/private key pair in Python so the wallet jargon starts to make sense.
- Sign and verify a message so you can visualize what happens when you click “Sign” in a wallet.
- Connect these pieces back to real wallets and basic safety practices.
You don’t need to be a developer to follow along, but you’ll get the most value if you’re comfortable copying code, running it, and reading the output.
You’ll need:
- A laptop with Python 3 installed.
- A browser wallet such as MetaMask.
- 30 minutes and a bit of curiosity.
Step 1 – Set up your tools
Before touching any crypto or private keys, get a small sandbox in place.
1.1 Install Python 3
python --versionIf this doesn’t show a version like Python 3.10.x, download and install Python from:
- Windows/macOS/Linux: https://www.python.org/downloads/
1.2 Create a small project folder
Pick or create a working folder, for example:
mkdir blockchain-crypto-democd blockchain-crypto-demo1.3 Install the cryptography library
pip install cryptographyYou now have everything you need to run the small code examples below.
Step 2 – Try a simple hash
Hashes are the backbone of blockchain immutability. In plain terms: they turn any message into a fixed-length “fingerprint,” and even tiny changes produce a completely different fingerprint.
Create a file called hash_demo.py in your project folder:
import hashlib
message = "Send 1 BTC to Alice"hash_output = hashlib.sha256(message.encode()).hexdigest()
print("Message:", message)print("Hash :", hash_output)Run it:
python hash_demo.pyNow change the message by one character (for example, Alice → Alic3) and run it again. The hash should be completely different. That sensitivity is what lets blockchains detect tampering quickly.
Key takeaway: hashes make it obvious when data has changed, even if you don’t look at the data itself.
Step 3 – Generate a key pair
Wallets talk about “public” and “private” keys, but it’s easier to grasp if you create one yourself.
Create a file keys_demo.py:
from cryptography.hazmat.primitives.asymmetric import ecfrom cryptography.hazmat.primitives import serialization
private_key = ec.generate_private_key(ec.SECP256K1())public_key = private_key.public_key()
private_pem = private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption(),)
public_pem = public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo,)
print("Private key (keep secret):\n", private_pem.decode())print("Public key (safe to share):\n", public_pem.decode())Run it:
python keys_demo.pyYou’ll see two blocks of text in PEM format. The private key is what your wallet protects behind a password or seed phrase. The public key is what ultimately becomes an address on networks like Ethereum and Bitcoin.
Key takeaway: a wallet is essentially a user-friendly wrapper around a private key; losing it means losing the ability to sign.
Step 4 – Sign and verify a message
Now you’ll tie hashing and keys together with a digital signature. This is the core of “proving it was you” on-chain.
Create a file signing_demo.py:
from cryptography.hazmat.primitives import hashesfrom cryptography.hazmat.primitives.asymmetric import ec
# 1. Generate a fresh key pair (like a wallet does behind the scenes)private_key = ec.generate_private_key(ec.SECP256K1())public_key = private_key.public_key()
message = b"Send 0.1 ETH to Alice"
# 2. Sign the message with the private keysignature = private_key.sign(message, ec.ECDSA(hashes.SHA256()))print("Signature bytes:", signature.hex())
# 3. Verify the signature with the public keypublic_key.verify(signature, message, ec.ECDSA(hashes.SHA256()))print("Signature verified!")Run it:
python signing_demo.pyYou should see “Signature verified!” at the end. If you change the message by even one character and run verification again without re-signing, it will fail. That’s exactly what happens if someone tries to alter a transaction after you sign it.
Key takeaway: a digital signature proves “this exact data came from this key holder and wasn’t changed afterward.”
Step 5 – Apply this to your wallet and daily security
Up to now you’ve seen everything “with the hood open.” In normal wallet use, the same pieces are there—they’re just packed behind a clean interface.
Here’s how what you just did maps to everyday actions:
-
Installing a wallet = setting up a key pair
When you install a wallet like MetaMask and create a new account, it quietly generates a private key and the matching public key for you. The 12 or 24‑word recovery phrase is just a human-friendly way to back up that private key. Losing it means losing your ability to sign, just like deleting theprivate_keyin your Python demo. -
Your address = a form of public key
The wallet derives an address from your public key (with some extra steps that depend on the chain). That’s what you copy-paste to receive funds. Anyone can see that address and check its activity—exactly like using the public key to verify a signature, but in a compressed, chain-specific format. -
Clicking “Sign” = running the signing demo behind the scenes
Each time you approve a transaction or message, your wallet:- Takes the relevant data (to, amount, contract call, or message).
- Hashes it (so a fixed “fingerprint” is signed, not arbitrary-length data).
- Signs that hash with your private key using an algorithm similar to the Python example.
The node or smart contract on the other side uses your public key/address to verify the signature. If anything in the data changes after you sign, verification fails.
-
Transaction hash = fingerprint of what you signed
After a transaction is broadcast, your wallet and block explorers show a transaction hash. That hash acts like a “receipt number” for the full transaction payload: from, to, value, and additional data. If you ever need to prove “this is the transaction I sent,” that hash is what you share.
Because all of this is wired into the wallet, you rarely think about hashes or elliptic curves in daily use—but knowing they’re there helps you spot when something feels off.
Practical safety habits:
- Never paste your seed phrase or private key into a website or chat—even to “test” something.
- Use hardware wallets for larger balances so the private key never leaves a secure device.
- Double-check the transaction details and domain before approving any signature, especially “Sign Message” or “Connect” prompts on new dApps.
If you keep this mental model in your head—hash → key pair → signature → transaction hash—wallet activity becomes less like magic and more like a series of clear steps you control. That makes it much easier to notice red flags and say “no” before a bad signature costs you real money.
Frequently Asked Questions
Do I need Python to understand cryptography?
No. The code samples are illustrative. You can follow the concepts without coding, or try them in a browser notebook later.
Is MetaMask required for this tutorial?
Any reputable wallet works. We mention MetaMask as an example because it's widely used and beginner friendly.