Introduction
Everything in this course so far travelled as plain bits that anyone on the path can read and change: a Wi-Fi neighbour, a router, a provider. Three things can go wrong. Someone can eavesdrop and read the data, tamper and change it, or impersonate a server and pretend to be your bank. Network security is the set of tools that stop these three. This lesson covers the building blocks and how TLS, the protocol under HTTPS, puts them together.
How it works
A hash function such as SHA-256 turns any data into a fixed-size fingerprint. The same data always gives the same hash, and changing a single character changes the whole hash. Anyone can compute a hash, so it detects accidental damage but does not stop an attacker: they change the message and compute a new hash.
An HMAC mixes a secret key into the hash. Only a sender who knows the key can make the right tag, so a matching tag proves two things: the message was not changed, and it came from someone with the key. Compare tags with hmac.compare_digest, which does not leak where they first differ.
import hashlib
import hmac
message = "transfer 100 to bob"
tampered = "transfer 900 to bob"
print("sha256: ", hashlib.sha256(message.encode()).hexdigest()[:16])
print("tampered:", hashlib.sha256(tampered.encode()).hexdigest()[:16])
key = b"shared-secret" # only the two sides know it
tag = hmac.new(key, message.encode(), hashlib.sha256).hexdigest()
for received in [message, tampered]:
check = hmac.new(key, received.encode(), hashlib.sha256).hexdigest()
verdict = "accepted" if hmac.compare_digest(tag, check) else "rejected"
print(received, "->", verdict)Encryption stops eavesdropping. Symmetric encryption uses one shared key and is fast, but two strangers have no shared key yet. Public-key cryptography solves this with a key pair: a public key anyone can know and a private key kept secret. It is slow, so it is used only to agree on a key.
How do you know a public key really belongs to your bank? A certificate is a statement "this key belongs to bank.example", signed by a certificate authority (CA). The CA's own certificate is signed by a higher CA, and so on up to a root that your system already trusts. This is a chain of trust. A browser checks that each certificate is valid today, that each is signed by the next, that the chain ends in a trusted root, and that the name matches the site.
TLS puts it together. The browser says hello and lists what it supports; the server sends its certificate chain; they use public-key methods to agree on a fresh secret key; from then on all data is encrypted and authenticated with fast symmetric keys. HTTPS is simply HTTP inside TLS.
Example
The example walks a certificate chain from a website up to its root and asks whether that root is trusted.
issuer = {"shop.example": "Mid CA", "Mid CA": "Root CA", "Root CA": "Root CA"}
trusted_roots = {"Root CA"}
name = "shop.example"
while True:
print(name, "is signed by", issuer[name])
if issuer[name] == name: # a root signs itself
break
name = issuer[name]
print("trusted root:", name in trusted_roots)Line by line
issuersays who signed each certificate; a root signs itself.trusted_rootsis the list the computer already trusts.- The loop prints each link, then moves up to the issuer, and stops at the certificate that signs itself.
- The last line checks that the root where the chain ends is trusted. If it were not in the list, the chain would be rejected.
Expected result
shop.example is signed by Mid CA
Mid CA is signed by Root CA
Root CA is signed by Root CA
trusted root: True
In the real world
The padlock in your browser means TLS worked: the chain was valid and the name matched. Websites renew their certificates every few months, often automatically, and an expired certificate is one of the most common causes of "your connection is not private" errors. Software updates and file downloads publish a hash so you can check that the file was not damaged or swapped. Messaging apps, VPNs and Wi-Fi security (WPA) all use the same pieces: a key agreement, then fast symmetric encryption. Attacks still happen at the edges, such as fake login pages and weak passwords, which no protocol can fix for you.
Common mistakes
- Thinking a hash proves who sent something. Anyone can hash a message; use an HMAC or a signature.
- Thinking HTTPS hides everything. The site name and the amount of data are still visible to the network.
- Clicking through certificate warnings. They mean the chain, the date or the name failed to check out.
- Comparing secrets with
==, which can leak information through timing. Usehmac.compare_digest.
Best practices
- Use HTTPS everywhere, and keep certificates renewed before they expire.
- Never invent your own encryption; use TLS and well-tested libraries.
- Keep keys and passwords out of code and out of messages.
- Check downloads against a published hash or signature.
Summary
Secure connections must stop eavesdropping, tampering and impersonation. Hashes detect change, HMACs add proof of the key holder, certificates chain a server's key to a trusted root, and TLS uses public-key methods to agree on a key and symmetric encryption to protect the data.
Quick check
Key takeaways
- A secure connection must stop eavesdropping, tampering and impersonation.
- A hash detects a change; an HMAC also proves the sender knew the secret key.
- A certificate chain ties a website's key to a trusted root, which proves who the server is.
- TLS uses public-key methods to agree on a key, then fast symmetric encryption for the data.
Practice problems for this lesson
Was this lesson clear?
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.