What is Fernet?
Fernet is Python's cryptography library's opinionated symmetric encryption recipe — it combines AES-128 in CBC mode with an HMAC-SHA256 authentication tag and a timestamp, so tokens are both encrypted and tamper-evident. The key itself is 32 random bytes, urlsafe-base64 encoded.
How to add it to your project
- Copy the generated key into your
.env:FERNET_KEY=<paste here> - Load and use it with
cryptography.fernet:from cryptography.fernet import Fernet import os f = Fernet(os.environ["FERNET_KEY"].encode()) token = f.encrypt(b"secret data") plaintext = f.decrypt(token) - Never hardcode the key in source and never commit
.env— rotate it via a secrets manager if it's ever exposed.
Verifying the format matches Fernet.generate_key()
>>> from cryptography.fernet import Fernet
>>> Fernet.generate_key()
b'aFOfD-hsobOnGocK_rALPLJxoo397Lo1_pweh-0lQKc='44 characters, urlsafe-base64 alphabet, single trailing = — identical shape to what this tool generates client-side.