A Python package that streamlines the process of encrypting and decrypting data with Fernet.
Project description
simple-fernet
A Python package that streamlines the process of encrypting and decrypting data with Fernet.
This package leverages user-specified Fernet keys stored in environment variables to simplify the process of encrypting and decrypting data with that key in Fernet. In addition, simple-fernet minimizes the amount of time that Fernet keys are held in-memory by only retrieving and using these keys when they're needed to encrypt or decrypt data.
Limitation Related to Environment Variables and Python
Use of simple-fernet requires that Fernet keys are both generated and stored in environment variables manually (before simple-fernet can be used). This is due to limitations around interfacing with system environment variables through Python, especially without requesting or providing privileged access.
Package maintainers are investigating ways to eliminate the need for these manual steps and will implement a solution if found. There are plans to develop a small companion tool to aid in the process of generating Fernet keys while this research is conducted.
For guidance on how to complete these manual steps (if you do not already have a Fernet key stored in environment variables), see the Quick Start below.
Features
- Initialize instance of
SimpleFernet, enabling encryption and decryption operations using a Fernet key stored in a user-provided environment variable. - Encrypt data.
- Decrypt data.
Ideas for a Future Release
- Add support for generating Fernet keys and storing them in system environment variables.
Quick Start
This quick start will guide you through the process of using simple-fernet in your Python projects.
This guide assumes the following:
- You have installed Python on the system where you intend to use
simple-fernet. - You have not yet installed the
cryptographyPython package. - You have not yet generated a Fernet key.
- You have not yet added a Fernet key to the system's environment variables.
With these assumptions in mind, this guide includes steps to generate a Fernet key and store it in environment variables for simple-fernet to use. If you have already generated a Fernet key and stored it in an environment variable, feel free to skip ahead in the guide.
If you have any feedback or suggestions to improve this guide, please don't hesitate to open an issue on GitHub!
macOS and Linux
- Install the
cryptographyPython package. On most systems, you can usepython -m pip install cryptographyto accomplish this. - Generate a Fernet key using the
fernetmodule of thecryptographyPython package. You can copy and paste the "Generate Fernet Key" script provided in this README's appendix into a new.pyfile and run it to do this quickly. - Copy the generated Fernet key.
- Open the profile file for your shell of choice (e.g.,
~/.bash_profile/~/.bashrcfor Bash or~/.zprofile/~/.zshrcfor Zsh). - Add a new line following like
export ENV_VARIABLE_NAME="FERNET KEY", whereENV_VARIABLE_NAMEis the name of your environment variable andFERNET KEYis the Fernet key you generated in step 2. - Save and close the profile file.
- Install the
simple-fernetPython package using a command likepython -m pip install simple-fernet. - In your Python code (that you want to use
simple-fernetin), add the following import statement:from simple_fernet import SimpleFernet - Initialize an instance of the
SimpleFernetclass, passing in the name of the environment variable you created as an argument (e.g.,sf = SimpleFernet('TEST_FERNET_KEY)). - Call the
encrypt()anddecrypt()methods of yourSimpleFernetinstance as needed to encrypt and decrypt data with the Fernet key you created.
Windows
- Install the
cryptographyPython package. On most systems, you can usepython -m pip install cryptographyto accomplish this. - Generate a Fernet key using the
fernetmodule of thecryptographyPython package. You can copy and paste the "Generate Fernet Key" script provided in this README's appendix into a new.pyfile and run it to do this quickly. - Copy the generated Fernet key.
- Open the Windows Start Menu, search for "Edit the system environment variables," and select the matching search result.
- In the System Properties window that opens, click the "Environment Variables" button.
- Click the "New" button in the "System variables" section.
- In the New System Variable window that appears, give the new environment variable a name and paste the Fernet key you generated in Step 2 into the "Variable value" field.
- Click "OK" until all three windows that you opened to edit the system's environment variables are closed.
- Install the
simple-fernetPython package using a command likepython -m pip install simple-fernet. - In your Python code (that you want to use
simple-fernetin), add the following import statement:from simple_fernet import SimpleFernet - Initialize an instance of the
SimpleFernetclass, passing in the name of the environment variable you created as an argument (e.g.,sf = SimpleFernet('TEST_FERNET_KEY)). - Call the
encrypt()anddecrypt()methods of yourSimpleFernetinstance as needed to encrypt and decrypt data with the Fernet key you created.
SimpleFernet Class
The SimpleFernet class is the heart of simple-fernet. Instances of the SimpleFernet class are used to facilitate data encryption and decryption with a pre-defined Fernet key (stored in an environment variable).
Initializer
The SimpleFernet initializer instantiates an instance of the SimpleFernet class and returns it so it can be used to encrypt and decrypt data.
Arguments
key_environment_variable: A string representing the name of an environment variable that contains a Fernet key. Please note that the Fernet key must be generated outside ofsimple-fernetand stored in environment variables beforeSimpleFernetcan use it (see above note on limitations of the package).
Returns
An initialized instance of the SimpleFernet class.
encrypt() Method
Converts the provided data to bytes, encrypts it using Fernet, and returns the encrypted data as bytes.
Arguments
data: The data to be encrypted.
Returns
A bytes object representing the encrypted data or None (if the encryption operation failed for some reason).
decrypt() Method
Decrypts the provided encrypted data using Fernet and returns it.
Arguments
encrypted_data: Abytesorstrvalue representing the data to decrypt with Fernet.
Returns
The decrypted data or None (if the decryption operation failed for some reason).
Notes
- The value passed in as
encrypted_datamust be of typebytesorstr. If not, aTypeErrorwill be raised.
Appendix
Script: Generate Fernet Key
This script provides a quick and easy way to generate a Fernet key.
Please note that this script requires that the cryptography Python package is installed in the environment where you run it.
When extracting the generated key, copy only the characters between b' and the closing single quote ('). For example, if this script prints b'6D9q48iAPS87jrz8zp5fiGj7VBGCyS9TGvJJ08QhkQ8=', only copy 6D9q48iAPS87jrz8zp5fiGj7VBGCyS9TGvJJ08QhkQ8=.
from cryptography.fernet import Fernet
print(Fernet.generate_key())
Script: Sample of How to Use simple-fernet in Python Code
This script is an extremely simple example of how to use simple-fernet in your Python code.
In this script, the simple-fernet package is imported, an instance of SimpleFernet is initialized, a simple list is established, encrypted, and decrypted.
from simple_fernet import SimpleFernet
sf = SimpleFernet(key_environment_variable='TEST_FERNET_KEY')
raw_data = ['Hello, world!', 67, {'Name': 'simple-fernet', 'isCool': True}]
print(f'Raw Data: {raw_data}')
encrypted_data = sf.encrypt(raw_data)
print(f'Encrypted Data: {encrypted_data}')
decrypted_data = sf.decrypt(encrypted_data)
print(f'Decrypted Data: {decrypted_data}')
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simple_fernet-1.0.1.tar.gz.
File metadata
- Download URL: simple_fernet-1.0.1.tar.gz
- Upload date:
- Size: 17.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ddbaf0cdf3354e808d4a96a02fe643e9c88242d005c9e5fda9e54eacf8608144
|
|
| MD5 |
d99ff460070d6620d313109cb4652ae7
|
|
| BLAKE2b-256 |
78e75dbbb640b03f2d97a67af724a1d97f37195b64e708e1170c97726e4fb0fc
|
File details
Details for the file simple_fernet-1.0.1-py3-none-any.whl.
File metadata
- Download URL: simple_fernet-1.0.1-py3-none-any.whl
- Upload date:
- Size: 17.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f594e18137953d49c2b873e39dce5460e7a3133621d87571faa494ed8b33d2f2
|
|
| MD5 |
cb69f62396a6745faf3e0e1803bf24b7
|
|
| BLAKE2b-256 |
a13300f304a5d603c5254c05adc145deb6856c460ec320af7675e33e0487e749
|