ENع
←Signatures
Lesson 09 · Cryptography

Digital signatures

12 min10 steps5 questionsIntermediate
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 9 · Interactive
01 / 10

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
Loading figure…
or ← →
Read the lesson step by step
  1. 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
  2. Alice 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 checks
  3. Bob 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)
  4. 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 hash
  5. Sign 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 msg
  6. Message 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 verifies
  7. Bob 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 key
  8. A 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 check
  9. Proof 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 leaks
  10. Where 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

Quiz Unlocks after the last step.