Using passwords with the fernet package

It is possible to use passwords with Fernet. To do this, you need to run the password through a key-derivation function, such as PBKDF2HMAC.

PBKDF2 (Password Based Key Derivation Function 2) is typically used for deriving a cryptographic key from a password.

More information about key derivation functions can be found at https://cryptography.io/en/latest/hazmat/primitives/key-derivation-functions/.

In this example, we are using this function to generate a key from a password, and we use that key to create the Fernet object we will use for encrypting and decrypting data. In this case, the data to encrypt is a simple message string. We can use the verify() method, which checks whether deriving a new key ...

Get Mastering Python for Networking and Security now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.