Install & Compatibility
Where this runs
tested against v1.0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.011s · 19.3MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 3.1s · import 0.010s · 20MB
17MB installed
● package 17MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Fernet
✓ from fernet import Fernet
✗ from cryptography.fernet import Fernet
This is the import for the pure Python 'fernet' package. For secure applications, you should use 'from cryptography.fernet import Fernet' from the 'cryptography' library.
This quickstart demonstrates how to generate a key, encrypt a message, and decrypt it using the `fernet` library. Keys and messages must be bytes. Remember to store your generated key securely, as anyone with the key can decrypt your data. This example is for demonstration; for production, use `cryptography.fernet`.
import os
from fernet import Fernet
# Generate a new Fernet key. This must be kept secret and securely stored.
# In a real application, you would load this from a secure configuration.
key = Fernet.generate_key()
print(f"Generated Key: {key.decode()}")
# Instantiate Fernet with the key
f = Fernet(key)
# Original message (must be bytes)
original_message = b"My super secret message"
# Encrypt the message
encrypted_message = f.encrypt(original_message)
print(f"Encrypted Message: {encrypted_message}")
# Decrypt the message
decrypted_message = f.decrypt(encrypted_message)
print(f"Decrypted Message: {decrypted_message.decode()}")
# Verify decryption
assert original_message == decrypted_message
print("Decryption successful!")
Debug
Known issues
breakingThis 'fernet' library (PyPI package 'fernet', GitHub 'oz123/python-fernet') is a *pure Python reference implementation* and is **NOT RECOMMENDED for production or security-sensitive applications**. The actively maintained, robust, and cryptographically secure Fernet implementation is provided by the `cryptography` library (import `from cryptography.fernet import Fernet`).fixFor any secure or production use case, switch to `cryptography.fernet`. Install with `pip install cryptography` and use `from cryptography.fernet import Fernet`.
affects: All versions
deprecatedThe `fernet` library by `oz123` was last updated in September 2016 and is not actively maintained for security patches or new features. Using an unmaintained cryptographic library can expose your application to known and unknown vulnerabilities.fixMigrate to `cryptography.fernet` which is actively developed and receives regular security audits and updates.
affects: All versions (since 1.0.1 was released in 2016)
gotchaThe `fernet` library's `encrypt()` and `decrypt()` methods strictly require `bytes` objects as input. Passing plain strings will result in a `TypeError` or incorrect behavior.fixEnsure all plaintext messages are encoded to bytes (e.g., `"hello".encode('utf-8')`) before encryption, and decrypted messages are decoded from bytes to string (e.g., `decrypted_message.decode('utf-8')`) for string manipulation. affects: All versions
Errors
Common errors & fixes
TypeError: token must be bytes.
The `decrypt()` method was called with a string instead of a bytes object.
fixEnsure the encrypted token passed to `decrypt()` is a bytes object. If you loaded it from storage, ensure it's read in binary mode (e.g., `rb`) and not decoded prematurely.
TypeError: key must be bytes.
The Fernet key provided during initialization (`Fernet(key)`) was a string instead of a bytes object.
fixEnsure the key is a bytes object. Keys are typically generated as bytes and should be handled as such. If loading from a file, read it in binary mode (`'rb'`).
ValueError: Fernet key must be 32 url-safe base64-encoded bytes.
The key used to initialize `Fernet()` is not the correct length (32 bytes) or is not properly URL-safe base64 encoded.
fixAlways use `Fernet.generate_key()` to create a valid key. If loading a key, ensure it hasn't been corrupted or modified and is still a 32-byte URL-safe base64-encoded bytes string.
Upgrade
Version history
1.0.1latest on PyPI · released Sep 18, 2016
Audit
Dependencies
No dependency data recorded yet.