Introduction
When your browser shows a lock next to https://, it is making two promises. The first, that nobody can read the connection, comes from the encryption of the last three lessons. The second is harder: that the server really is the one whose name is in the address bar, and not an attacker in the middle. That promise comes from digital signatures and certificates.
A signature does for data what a signature on a result sheet tries to do on paper: it shows who approved it, and that nothing was changed afterwards. Unlike ink, a digital signature cannot be copied onto a different document.
How it works
A digital signature turns RSA around. To sign, the owner uses the private key; to verify, anyone uses the matching public key.
- Compute a hash of the message: a short number that depends on every bit of it.
- Sign the hash:
s = hashᵈ mod n, using the private exponentd. - Send the message with
s. - The receiver computes
sᵉ mod nwith the public key and compares it with their own hash of the message. Equal means valid.
Only the holder of d can make an s that passes, so a valid signature proves who signed. If even one character of the message changes, its hash changes and the check fails, so the signature also proves integrity. This gives non-repudiation too: the signer cannot later deny signing, because no one else could have.
But a public key is just a number. How do you know that a key really belongs to portal.example.edu? A certificate answers this. It is a small document that says "this public key belongs to this name", signed by a Certificate Authority (CA). The CA's own certificate is signed by a higher CA, up to a root CA whose certificate is already built into your browser or operating system. Checking a website means walking this chain of trust from the site up to a root you already trust.
// A certificate chain: each certificate is signed by the one above it, up to a root the browser already trusts.
const certificates = {
"portal.example.edu": { issuer: "Campus Issuing CA", signatureOk: true },
"Campus Issuing CA": { issuer: "Trusted Root CA", signatureOk: true },
"Trusted Root CA": { issuer: "Trusted Root CA", signatureOk: true },
};
const trustedRoots = ["Trusted Root CA"];
function check(name) {
const path = [];
let current = name;
while (true) {
const cert = certificates[current];
if (!cert) return path.concat(current).join(" <- ") + ": unknown certificate";
path.push(current);
if (!cert.signatureOk) return path.join(" <- ") + ": bad signature";
if (trustedRoots.includes(current)) return path.join(" <- ") + ": trusted";
if (path.length > 5) return path.join(" <- ") + ": chain too long";
current = cert.issuer;
}
}
console.log(check("portal.example.edu"));
certificates["Campus Issuing CA"].signatureOk = false;
console.log(check("portal.example.edu"));The first check walks portal → issuing CA → root and finds a root it trusts. In the second, the issuing CA's signature is broken, so the chain fails there and the browser would show a warning.
Example
The toy RSA keys from two lessons ago (n = 3233, e = 17, d = 2753) sign a results message. A toy hash squeezes the message into a number below n.
// A digital signature with the toy RSA keys: sign with the PRIVATE key d, check with the PUBLIC key e.
const n = 3233, e = 17, d = 2753;
function modPow(base, exp, m) {
let result = 1;
base = base % m;
while (exp > 0) {
if (exp % 2 === 1) result = (result * base) % m;
base = (base * base) % m;
exp = Math.floor(exp / 2);
}
return result;
}
// A toy hash that squeezes any message into a number below n (real systems use SHA-256).
function hash(text) {
let h = 0;
for (let k = 0; k < text.length; k++) h = (h * 31 + text.charCodeAt(k)) % n;
return h;
}
const message = "Result: CSE-2101 passed";
const signature = modPow(hash(message), d, n);
console.log("hash " + hash(message) + ", signature " + signature);
function verify(text, sig) {
return modPow(sig, e, n) === hash(text) ? "valid" : "INVALID";
}
console.log("original: " + verify(message, signature));
console.log("tampered: " + verify("Result: CSE-2101 failed", signature));Line by line
hashwalks the message like the checksum from the first lesson, keeping the result belown = 3233. The message hashes to 3101.- The signature is
3101²⁷⁵³ mod 3233 = 1227, made with the private exponentd. verifycomputes1227¹⁷ mod 3233with the public exponent and compares it with the message's hash. They match:valid.- The tampered message ("failed" instead of "passed") has a different hash, so the same signature no longer matches:
INVALID.
Expected result
hash 3101, signature 1227
original: valid
tampered: INVALID
The toy hash is weak: with only 3233 possible values, an attacker could find another message with the same hash and reuse the signature. Real signatures use a cryptographic hash such as SHA-256, where finding two messages with the same hash is infeasible. The next module covers hashes properly.
In the real world
- Every HTTPS certificate is checked this way, thousands of times a day on your own device.
- Free CAs such as Let's Encrypt made certificates automatic, which is a big reason most of the web now uses HTTPS.
- Phone apps, operating-system updates and software packages are signed, so your device can refuse code that was changed after the developer released it.
- Certificates expire and can be revoked if a private key is stolen, so browsers check that a certificate is still valid, not just well signed.
Common mistakes
- Mixing up which key does what. Encrypt with the receiver's public key; sign with your own private key.
- Clicking through certificate warnings. A warning often means the chain is broken, which is exactly what a man in the middle looks like.
- Signing a message instead of its hash. Real schemes sign a hash with padding; signing raw data has known weaknesses.
- Trusting a self-signed certificate on a public site. Anyone can sign their own certificate; it proves nothing about who they are.
Best practices
- Use your platform's TLS and signature libraries, which check chains, expiry and revocation for you.
- Protect private signing keys more than anything else: whoever holds them can impersonate you.
- Renew certificates automatically, before they expire.
- Treat every certificate warning as a possible attack until you know its cause.
Summary
A digital signature is made with a private key and checked with the public key, so it proves who signed and that the data has not changed since. Certificates bind a public key to a name and are signed by Certificate Authorities, forming a chain up to a root your device already trusts. Together, signatures and certificates stop the man-in-the-middle attack that a plain key exchange cannot, and they are the second half of the lock in your browser's address bar.
Quick check
Key takeaways
- Sign with your private key; anyone verifies with your public key.
- A valid signature proves who signed and that the data has not changed.
- Certificates bind public keys to names, and chains lead to roots built into your device.
- Signatures and certificates are what stop a man in the middle in TLS.
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.