A concise, copy-paste-ready blog page that explains the TLS handshake and embeds runnable code examples (for demonstration). Includes a small visual diagram and copy buttons for each snippet so your readers can try examples quickly.
What is TLS (short)
TLS (Transport Layer Security) is the modern protocol that secures HTTP connections (you know it as HTTPS). The handshake is the initial exchange where client and server agree on versions, cipher suites, and derive shared secrets for encryption.
Handshake diagram
Quick analogy
Imagine two people meeting: they say hello, show ID, verify identity with a trusted third party, agree a secret handshake, and then speak in private. That sequence is the TLS handshake.
Code examples
1) Observe a real TLS connection (Python)
import socket, ssl
hostname = 'www.google.com'
port = 443
context = ssl.create_default_context()
with socket.create_connection((hostname, port)) as sock:
with context.wrap_socket(sock, server_hostname=hostname) as secure_sock:
print(f"Connected to {hostname} with {secure_sock.version()}")
print("Cipher used:", secure_sock.cipher())
print("Server certificate:", secure_sock.getpeercert())
2) Simple Diffie-Hellman demo (educational)
# Educational DH demo — not secure for production
import random
P = 23 # small prime for demo
G = 5
a = random.randint(1, P-1)
A = pow(G, a, P)
b = random.randint(1, P-1)
B = pow(G, b, P)
client_secret = pow(B, a, P)
server_secret = pow(A, b, P)
print("Client public key:", A)
print("Server public key:", B)
print("Shared secret (client):", client_secret)
print("Shared secret (server):", server_secret)
3) Use OpenSSL to inspect handshake
openssl s_client -connect www.google.com:443
How to use this HTML
- Copy the full HTML file into
index.html. - Create a
style.cssfile for CSS and ascript.jsfile for JavaScript. - Open
index.htmlin your browser to preview the page. - Host it on GitHub Pages, Netlify, or Vercel to publish quickly.