Cryptography Overview
Cryptography is the practice of protecting information by transforming it so that only the right parties can read or trust it. Four properties matter in practice: confidentiality (only intended readers), integrity (data was not altered), authenticity (the sender is who they claim to be), and non-repudiation (the sender cannot deny the message). Every tool in this tutorial exists to deliver one or more of these properties.
What Python Offers
The standard library provides hashlib (digests, HMAC, PBKDF2), secrets (cryptographically secure random values), and ssl (TLS sockets). The third-party cryptography package - the de-facto standard library for modern cryptography in Python - adds AES, ChaCha20, Fernet, RSA, elliptic curves, X.509 certificates, and serialization for keys and signed data. This tutorial is built on hashlib plus cryptography.
Tool Map
| Goal | Tool | Chapter |
|---|---|---|
| Fingerprint / integrity | SHA-256 digests | Hashing Data |
| Store passwords | Argon2 / PBKDF2 | Password Hashing, Key Derivation |
| Authenticate messages | HMAC | HMAC and MAC |
| Encrypt small data | Fernet (AES + HMAC) | Fernet |
| Encrypt files/buffers | AES-GCM, ChaCha20 | AES chapters |
| Sign data / keys | RSA, Ed25519 | RSA and Signature chapters |
| Secure channels | TLS via ssl | TLS and Certificates |
Golden Rules Before You Start
- Do not invent or tweak a cipher. Use the primitives exactly as designed, with their recommended modes and parameters.
- Never reuse a key for two different purposes, and never reuse an initialization vector (IV) or nonce with the same key in a mode that forbids it.
- Prefer authenticated encryption (AES-GCM, ChaCha20-Poly1305, Fernet) over raw block ciphers, so tampering is detected, not just hidden.
- Keys are the whole game: generate them with a secure random source, store them outside the code, and rotate them when the holders change.
Summary
Map each requirement to a standard tool - digests for integrity, KDFs for passwords, AEAD for encryption, signatures for authenticity, TLS for channels - and lean on the standard library plus cryptography rather than on home-grown constructions.