According to the documentation for the cryptography.fernet
module, fernet keys are:
A URL-safe base64-encoded 32-byte key
Yet this doesn't work:
import secrets
from cryptography import fernet
f = fernet.Fernet(secrets.token_urlsafe(32))
failing with ValueError: Fernet key must be 32 url-safe base64-encoded bytes
- however the documentation for token_urlsafe
claims that it returns
a random URL-safe text string, containing nbytes random bytes. The text is Base64 encoded ...
Likewise, this doesn't work:
import base64
from cryptography import fernet
key = fernet.Fernet.generate_key()
print(base64.b64decode(key))
failing with: binascii.Error: Incorrect padding
.
So what is a Fernet key and what is the right way to go about generating one from a pre-shared string?