Lab 2 of 8 · Security fundamentals
Hashes, password storage and SSH keys
See how hashes prove integrity, why bcrypt beats plain hashing for passwords, and set up SSH key sign-in to GitHub.
- Time
- 45–60 minutes
- Cost
- Free.
What you learn
Confidentiality, integrity and availability are the three things security protects. Hashing, encryption and strong authentication are the everyday tools for doing it.
- The CIA triad: confidentiality, integrity, availability
- Hashing versus encryption
- Why passwords are stored with slow, salted hashes
- Keys for authentication: SSH and multi-factor sign-in
In the AI era: Attackers use AI to write convincing phishing and to crack weak passwords faster. Strong hashing and multi-factor authentication matter more than ever.
Before you start
Finish lab 1. Install Python 3.11 or newer.
Step by step
1 Set up Python for the whole roadmap
This file lists everything the later labs need.
flask==3.1.3 bcrypt==5.0.0 requests==2.34.2 httpx==0.28.1 pytest==8.3.42 Install it in a virtual environment
Run
source .venv/bin/activateagain whenever you open a new terminal.python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txt printf '.venv/\n__pycache__/\n.pytest_cache/\nlogs/\n.env\n' > .gitignore3 Prove a file has not changed
A hash is a fingerprint: change one character and it changes completely. Download sites publish hashes so you can check files.
echo "invoice total: 100" > invoice.txt shasum -a 256 invoice.txt echo "invoice total: 900" > invoice.txt shasum -a 256 invoice.txt rm invoice.txtYou should see:Two completely different hashes for a one-character change.
4 Compare password storage
Plain SHA-256 gives the same hash every time, so stolen hashes can be looked up in seconds. bcrypt adds a random salt and is deliberately slow.
import hashlib import bcrypt password = b"correct-horse-battery-staple" print("sha256 #1:", hashlib.sha256(password).hexdigest()) print("sha256 #2:", hashlib.sha256(password).hexdigest()) first = bcrypt.hashpw(password, bcrypt.gensalt()) second = bcrypt.hashpw(password, bcrypt.gensalt()) print("bcrypt #1:", first.decode()) print("bcrypt #2:", second.decode()) print("right password accepted:", bcrypt.checkpw(password, first)) print("wrong password accepted:", bcrypt.checkpw(b"guess", first))You should see:Identical SHA-256 lines, two different bcrypt lines, then
TrueandFalse.5 Create an SSH key and add it to GitHub
Keys replace passwords for machines. The private key never leaves your computer; you give GitHub only the
.pubfile. Press Enter to accept the default file and choose a passphrase.ssh-keygen -t ed25519 -C "home-soc-lab" cat ~/.ssh/id_ed25519.pubYou should see:A line starting
ssh-ed25519. Copy it into GitHub → Settings → SSH and GPG keys → New SSH key.6 Test the key and switch the repository to SSH
From now on, pushes use your key instead of a token. Replace YOUR-USERNAME.
ssh -T git@github.com git remote set-url origin git@github.com:YOUR-USERNAME/home-soc-lab.gitYou should see:Hi YOUR-USERNAME! You've successfully authenticated.7 Turn on multi-factor sign-in
In GitHub → Settings → Password and authentication, enable two-factor authentication with an authenticator app or a passkey. Do the same for your email account.
8 Commit and push over SSH
Add a short
notes/02-fundamentals.mdexplaining hashing versus encryption in your own words first.git add requirements.txt .gitignore password_storage.py notes git commit -m "Add hashing and password storage demo" git push
You are done when
You can explain why the same password gave two different bcrypt hashes.
Your pushes use your SSH key.
Two-factor authentication is on for GitHub.
We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.