Digital signatures
About this lesson
A digital signature lets anyone holding your public key check two things about a message: that it was signed with your private key, and that it has not changed since. Follow one real example in Python: Alice makes an Ed25519 key pair, signs "Pay Bob 10", and Bob verifies it. Then watch a copy changed to "Pay Bob 99" fail the check, and learn what signatures prove, what they do not, and the mistakes that undo them.
Can Bob trust this message?
Alice sends Bob "Pay Bob 10" over the internet. Bob cannot see who sent it, or whether someone changed it on the way. A digital signature lets him check both: who signed it (authenticity) and that it is unchanged (integrity).
message "Pay Bob 10", sent in the open question 1 did Alice really send it? (authenticity) question 2 is it exactly what she sent? (integrity) tool a digital signature answers both
Read the lesson step by step
Can Bob trust this message?
Alice sends Bob "Pay Bob 10" over the internet. Bob cannot see who sent it, or whether someone changed it on the way. A digital signature lets him check both: who signed it (authenticity) and that it is unchanged (integrity).
message "Pay Bob 10", sent in the open question 1 did Alice really send it? (authenticity) question 2 is it exactly what she sent? (integrity) tool a digital signature answers bothAlice makes a key pair
Signatures use public-key cryptography. Line 1 makes a private key, which Alice keeps secret: only it can sign. Line 2 derives the matching public key, which anyone may have: it can only check. Each Ed25519 key is 32 bytes.
the whole program (Python, pip install cryptography): from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey from cryptography.exceptions import InvalidSignature priv = Ed25519PrivateKey.generate() # Alice keeps this secret pub = priv.public_key() # Alice shares this msg = b"Pay Bob 10" sig = priv.sign(msg) # 64 bytes pub.verify(sig, msg) # returns None: valid try: pub.verify(sig, b"Pay Bob 99") except InvalidSignature: print("rejected: changed or not signed by Alice") private key 32 bytes, secret: signs public key 32 bytes, shared: only checksBob gets the public key
Alice gives Bob her public key ahead of time. Sharing it is safe: it cannot sign, and the private key cannot practically be worked out from it. Bob must be sure the key really is Alice's; certificates exist to vouch for that.
public key safe to publish cannot sign anything does not reveal the private key but Bob must know it is really Alice's (a certificate vouches for who owns a key)A hash: the message fingerprint
Line 3 stores the message as bytes. Signing runs it through a hash function, which turns any input into a short, fixed-size fingerprint. Change one character and the fingerprint changes completely. Ed25519 uses SHA-512 inside, a 64-byte hash.
hash function any input -> fixed-size fingerprint SHA-512 64 bytes out (FIPS 180-4) first 8 bytes of SHA-512, as a demonstration: "Pay Bob 10" 0c63f50b875b2f17… "Pay Bob 99" 50d656aa1bac3c56… one character changed, a completely different hashSign with the private key
Line 4 signs. The maths mixes the message's hash with Alice's private key. Out comes the signature: 64 bytes that only her private key could make, for exactly this message. Signing does not hide anything: the message stays readable.
sig = priv.sign(msg) signature 64 bytes, starts 809a4b09f18f602c… (RFC 8032 test key, so it is reproducible) tied to this exact message + this private key not encryption: anyone can still read msgMessage and signature travel
Alice sends the message with the signature attached. Both cross the internet in the open; nothing here is secret. The protection is that nobody without her private key can make a signature that matches a message.
sent "Pay Bob 10" + 64-byte signature secret? no, both are public protection only the private key can make a signature that verifiesBob verifies: valid
Line 5 checks message, signature and Alice's public key against each other. They match, so verify() simply returns. Bob now knows the message is unchanged and was signed with Alice's private key.
pub.verify(sig, msg) inputs message + signature + public key result returns None: no error = valid Bob knows unchanged, and signed with the key that matches Alice's public keyA changed message fails
Suppose someone on the way changes 10 to 99 and keeps the signature. The new message no longer matches it, so line 6 raises InvalidSignature and Bob rejects it. Making a new matching signature would need Alice's private key.
pub.verify(sig, b"Pay Bob 99") message changed on the way signature still the one for "Pay Bob 10" result raises InvalidSignature Bob rejects it: never act on a failed checkProof Alice signed it
Only Alice holds the private key, so a valid signature is evidence she signed. She cannot easily deny it later: NIST calls this non-repudiation. It holds only while the key stays secret, so guard it, ideally in hardware, and replace it if it leaks.
non-repudiation the signer cannot easily deny it depends on the private key staying secret protect it hardware key store, few people, replace (revoke) it if it leaksWhere signatures protect you
Software updates, HTTPS certificates and signed code all rely on this. NIST FIPS 186-5 approves RSA, ECDSA and EdDSA. Common mistakes: skipping the check, carrying on after it fails, or trusting a public key without knowing whose it is.
used for software updates, code signing, HTTPS certificates, signed email approved RSA, ECDSA, EdDSA (FIPS 186-5) mistakes no verify step at all ignoring a failed verify a public key of unknown owner
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01Alice signs a message. Which key does Bob need to verify the signature?
The private key signs; the matching public key verifies. Bob only ever needs Alice's public key, which is why it is safe for her to share it.
02Someone changes a signed message from "Pay Bob 10" to "Pay Bob 99" but keeps the original signature. What happens when Bob runs pub.verify(sig, msg)?
A signature is tied to the exact message. Any change makes the check fail, and a new matching signature would need Alice's private key.
03Does signing a message keep it secret?
Signatures give integrity and authenticity, not confidentiality. To keep a message secret you also need encryption.
04Why can a digital signature give non-repudiation when a shared-key MAC (such as HMAC) cannot?
With a MAC, sender and receiver share one key, so the receiver could have produced the tag too. Only the private-key holder can make a valid signature.
05A program downloads an update and its signature, but carries on installing when verify() fails. What is the mistake?
A signature only protects you if the check is done and a failure stops the action. OWASP lists this under software and data integrity failures.
Glossary
- Digital signature
- A value made from a message and a private key that anyone with the matching public key can check. It shows the message is unchanged (integrity) and was signed with that private key (authenticity).
- Private key and public key
- A matched pair. The private key is kept secret and is the only key that can sign; the public key is shared and can only verify. The private key cannot practically be worked out from the public one.
- Hash function
- A function that turns any input into a short, fixed-size fingerprint, such as SHA-512's 64 bytes. Changing one character changes the hash completely.
- Verify
- Checking a message, a signature and a public key against each other. In Python's cryptography library, verify() returns None when they match and raises InvalidSignature when they do not.
- Non-repudiation
- Evidence that a particular signer made a signature, so they cannot easily deny it later. It depends on the private key staying secret.
- Ed25519
- A modern signature scheme (EdDSA on the edwards25519 curve) with 32-byte keys and 64-byte signatures. NIST FIPS 186-5 approves it alongside RSA and ECDSA.
- Certificate
- A signed document that vouches that a public key belongs to a particular person or organisation, so you know whose key you are trusting.
Notes and sources
- Ed25519 does not sign one hash of the message. RFC 8032 hashes the message twice with SHA-512, mixed with key material, inside the signing maths; the figure draws this as one "hash, then sign" box.
- Signing is often described as "encrypting the hash with the private key". That loosely fits textbook RSA but not ECDSA or EdDSA, and real RSA signatures use padding schemes. Signing and encryption are separate operations.
- A signature only proves the message matches a public key. Knowing whose key it is is a separate problem, solved by certificates and public key infrastructure (PKI), or by checking the key in person.
- A message authentication code (MAC, such as HMAC) also proves integrity, but sender and receiver share one secret key, so either could have made it: a MAC cannot give non-repudiation.
- The figure uses RFC 8032's published test key so its numbers are reproducible. It is public: never use a test key for anything real; generate your own as in line 1.
- This course is about defence. It explains weaknesses at the level of the mistake and the fix, never how to attack a real system.
- NIST FIPS 186-5: Digital Signature Standard (DSS), February 2023
- NIST FIPS 180-4: Secure Hash Standard (SHS)
- NIST SP 800-57 Part 1 Rev. 5: Recommendation for Key Management
- NIST CSRC Glossary: digital signature
- NIST CSRC Glossary: non-repudiation
- RFC 8032: Edwards-Curve Digital Signature Algorithm (EdDSA)
- pyca/cryptography: Ed25519 signing
- CISA: Understanding Digital Signatures
- CISA, NSA and ODNI: Securing the Software Supply Chain, Recommended Practices for Developers
- OWASP Top 10:2025 A08: Software or Data Integrity Failures
Quiz Unlocks after the last step.