|
Some checks are pending
Lint & Test / lint (push) Waiting to run
Lint & Test / test (4.2, 3.10) (push) Waiting to run
Lint & Test / test (4.2, 3.11) (push) Waiting to run
Lint & Test / test (4.2, 3.12) (push) Waiting to run
Lint & Test / test (4.2, 3.13) (push) Waiting to run
Lint & Test / test (4.2, 3.14) (push) Waiting to run
Lint & Test / test (5.1, 3.10) (push) Waiting to run
Lint & Test / test (5.1, 3.11) (push) Waiting to run
Lint & Test / test (5.1, 3.12) (push) Waiting to run
Lint & Test / test (5.1, 3.13) (push) Waiting to run
Lint & Test / test (5.1, 3.14) (push) Waiting to run
Lint & Test / test (5.2, 3.10) (push) Waiting to run
Lint & Test / test (5.2, 3.11) (push) Waiting to run
Lint & Test / test (5.2, 3.12) (push) Waiting to run
Lint & Test / test (5.2, 3.13) (push) Waiting to run
Lint & Test / test (5.2, 3.14) (push) Waiting to run
Lint & Test / test (6.0, 3.12) (push) Waiting to run
Lint & Test / test (6.0, 3.13) (push) Waiting to run
Lint & Test / test (6.0, 3.14) (push) Waiting to run
* remove non-tested django versions from the list of tested versions in the readme * add snippet on generating SECRET_KEYs * add contributing readme * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add file filter for github actions lint-and-test workflow * add path filters for the lint and test github actions workflow * use path-ignore instead of path for file filters --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> |
||
|---|---|---|
| .github/workflows | ||
| encrypted_fields | ||
| package_test | ||
| .gitignore | ||
| .pre-commit-config.yaml | ||
| CONTRIBUTING.md | ||
| LICENCE.txt | ||
| manage.py | ||
| pyproject.toml | ||
| README.md | ||
| requirements.txt | ||
| setup.py | ||
Django Fernet Encrypted Fields
This package was created as a successor to django-encrypted-fields.
Getting Started
$ pip install django-fernet-encrypted-fields
In your settings.py, set random SALT_KEY
SALT_KEY = '0123456789abcdefghijklmnopqrstuvwxyz'
Then, in models.py
from encrypted_fields.fields import EncryptedTextField
class MyModel(models.Model):
text_field = EncryptedTextField()
Use your model as normal and your data will be encrypted in the database.
Rotating SALT keys
You can rotate salt keys by turning the SALT_KEY settings.py entry into a list. The first key will be used to encrypt all new data, and decryption of existing values will be attempted with all given keys in order. This is useful for key rotation: place a new key at the head of the list for use with all new or changed data, but existing values encrypted with old keys will still be accessible
SALT_KEY = [
'zyxwvutsrqponmlkjihgfedcba9876543210',
'0123456789abcdefghijklmnopqrstuvwxyz'
]
To generate a new SECRET_KEY, you can use Django's get_random_secret_key function.
from django.core.management.utils import get_random_secret_key
new_secret_key = get_random_secret_key()
print(new_secret_key)
Rotating SECRET_KEY
When you would want to rotate your SECRET_KEY, set the new value and put your old secret key value to SECRET_KEY_FALLBACKS list. That way the existing encrypted fields will still work, but when you re-save the field or create new record, it will be encrypted with the new secret key. (supported in Django >=4.1)
SECRET_KEY = "new-key"
SECRET_KEY_FALLBACKS = ["old-key"]
If you wish to update the existing encrypted records simply load and re-save the models to use the new key.
for obj in MyModel.objects.all():
obj.save()
Available Fields
Currently built-in and unit-tested fields include the following. They have the same APIs as their non-encrypted counterparts.
EncryptedCharFieldEncryptedTextFieldEncryptedDateTimeFieldEncryptedIntegerFieldEncryptedFloatFieldEncryptedEmailFieldEncryptedBooleanFieldEncryptedJSONField
Compatible Django Version
| Compatible Django Version | Specifically tested | Python Version Required |
|---|---|---|
3.2 |
3.8+ | |
4.0 |
3.8+ | |
4.1 |
3.8+ | |
4.2 |
✔️ | 3.8+ |
5.0 |
3.10+ | |
5.1 |
✔️ | 3.10+ |
5.2 |
✔️ | 3.10+ |
6.0 |
✔️ | 3.12+ |
Contributing
See CONTRIBUTING.md for details on how to contribute to this project.
