To generate and verify a digital signature in Python, create a key pair with the cryptography package, sign the message bytes with the private key, and verify the signature with the public key. A failed check raises InvalidSignature, so verification is always wrapped in try/except.
For new systems, Ed25519 is the simplest and safest choice: it has no padding or hash options to get wrong. RSA with PSS padding and SHA-256, and ECDSA on the P-256 curve, remain widely used when you need compatibility.
A signature proves two things: the message was produced by the holder of the private key (authenticity), and it has not changed since (integrity). The signer hashes the message and transforms the hash with the private key; anyone with the public key can check the result. The private key never leaves the signer.
Install the library, which is the standard choice for this task:
Ed25519 signs the raw message directly, so you do not choose a hash or padding.
verify() returns None on success and raises on failure, so never treat a missing exception handler as "valid". The signature is 64 bytes for Ed25519.
Use RSA when a protocol or partner requires it. Use at least a 2048-bit key (3072 bits is a common choice for long-lived keys), PSS padding and SHA-256. The older PKCS#1 v1.5 padding still verifies but PSS is preferred for new code.
ECDSA gives much smaller keys and signatures than RSA at a similar security level.
To verify later or on another machine, serialise the keys. Protect the private key with a password.
In production, load the passphrase from a secret manager or environment variable, not from source code, and prefer a KMS or HSM for high-value keys.
Sign the file bytes, and store the signature next to the file. For very large files, hash with hashes.Hash in chunks and sign the digest using Prehashed (RSA and ECDSA support this; Ed25519 does not).
| Mistake | Better approach |
|---|---|
| Treating a hash as a signature | A plain hash can be recomputed by anyone; sign with a private key |
Using the rsa key size 1024 | Use 2048 bits at minimum |
Ignoring InvalidSignature | Always catch it and reject the message |
| Hard-coding private keys | Use a secret manager, KMS or encrypted PEM files |
| Signing a parsed object | Sign a canonical byte form, such as the exact bytes you transmit |
Use Ed25519 for new systems with no compatibility constraints. Choose RSA-PSS or ECDSA when a standard, certificate chain or partner requires them.
Load the public key with load_pem_public_key() and call verify(signature, message, ...) with the same algorithm parameters used to sign. An InvalidSignature exception means the check failed.
No. A signature proves origin and integrity but does not hide the message. To keep the message secret, encrypt it separately.
No. PyCrypto is unmaintained. Use cryptography, or PyNaCl for Ed25519 if you prefer its simpler API.
Generate a key pair, sign bytes with the private key, and call verify() with the public key inside a try/except InvalidSignature block. Prefer Ed25519 for new work, use RSA-PSS with SHA-256 or ECDSA when you must, store keys encrypted, and never reuse a signing key for other purposes.