
Research
Security News
The Growing Risk of Malicious Browser Extensions
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
django-encrypted-field
Advanced tools
This is a Django Model Field class that can be encrypted using ChaCha20 poly 1305, and other algorithms.
Django custom field supporting different encryption options.
By default, this field will fall back to ChaCha20 Poly 1305 algorithm, as we consider the stronger one to have.
But the user has some other options to configure.
There exist a pip package in the registry. Just issue the typical "install" command:
$ pip install django-encrypted-field
Before using the EncryptedField in your projects, it is necessary to add some configuration variables to your settings. Please, remember to do so, as this is CRITICAL to have the maximum guarantees in terms of encryption.
b'12345...'
.See an example:
DJANGO_ENCRYPTED_FIELD_KEY = b'12345678901234567890123456789012'
# Recommended: using the environment.
DJANGO_ENCRYPTED_FIELD_KEY = os.environ.get('ENV_DJANGO_ENCRYPTED_FIELD_KEY')
DJANGO_ENCRYPTED_FIELD_ALGORITHM = 'CC20P'
DJANGO_ENCRYPTED_FIELD_ALGORITHM = 'SS20'
...
DJANGO_ENCRYPTED_FIELD_ALGORITHM = 'AGCM'
The use of the custom field is easy. You don't need to add the packaged to the INSTALLED_APPS, so just include an import in your models and use the field directly.
For example, if you want to start the easy way, with the default encryption (ChaCha20 Poly 1305), follow these steps:
Just configure the key:
DJANGO_ENCRYPTED_FIELD_KEY = os.environ.get('ENV_DJANGO_ENCRYPTED_FIELD_KEY')
Take on mind the following restrictions:
Now, import the field and add it to your very secret model:
from django.db import models
from encrypted_field import EncryptedField
class MySecretModel(models.Model):
secret = EncryptedField()
Just use as any other field, but with these restrictions:
See the usage in a helper script (not a Django view). Encryption (just save):
# -*- coding: utf-8 -*-
#!/usr/bin/python
import os
import sys
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
from django.conf import settings
django.setup()
from app.models import MySecretModel
secret_instance = MySecretModel()
secret_instance.secret = 'A very secret message we want to store in database.'
secret_instance.save()
Decryption (just query the model):
# -*- coding: utf-8 -*-
#!/usr/bin/python
import os
import sys
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
from django.conf import settings
django.setup()
from app.models import MySecretModel
secret_instance = MySecretModel.objects.get(id=1)
print(
"The SECRET=[{secret}]".format(secret=secret_instance.secret)
)
The previous example is the quick&easy way of using this custom field. But you may want to customize the way it will work.
As for the present release, the following algorithms are supported:
The assigned text is a short name in text for the algorithm, to pass it in dictionaries and JSON objects, and is the value you should use if going to set the settings variable (remember, DJANGO_ENCRYPTED_FIELD_ALGORITHM = 'AGCM''
).
It is VERY IMPORTANT to define the variable if you are changing the algorithm in the field definition, as we will see below. Please, do remember this.
When adding the field to the model, you can change the default algorithm if necessary. Just passing "algorithm" in the field definition:
from django.db import models
from encrypted_field import EncryptedField
class MySecretModel(models.Model):
secret = EncryptedField(algorithm='SS20') # Will use Salsa20 algorithm.
You may want to make more difficult to attack the encryption just removing algorithm information from the database:
from django.db import models
from encrypted_field import EncryptedField
class MySecretModel(models.Model):
secret = EncryptedField(algorithm='SS20', hide_algorithm=True) # Will use Salsa20 algorithm. HIDDEN.
So the encrypted results will be stored in the database without any reference to the algorithm that was used. If this is a use case you need, PLEASE REMEMBER TO SET THE SETTINGS VARIABLE FOR THE ALGORITHM.
In your_project/settings.py:
DJANGO_ENCRYPTED_FIELD_KEY = os.environ.get('ENV_DJANGO_ENCRYPTED_FIELD_KEY')
DJANGO_ENCRYPTED_FIELD_ALGORITHM = 'AGCM'
In app/models.py:
from django.db import models
from encrypted_field import EncryptedField
class MySecretModel(models.Model):
secret = EncryptedField(algorithm='AGCM', hide_algorithm=True) # Will use AGCM algorithm. HIDDEN.
If you want to change the default prepend header for some algorithms, you can pass a new header onto the field definition. See:
from django.db import models
from encrypted_field import EncryptedField
class MySecretModel(models.Model):
secret = EncryptedField(header='My custom header')
There is no way to set the key in the field, so the key is never used in a persistent way. Instead, everytime time an encryption/decryption operation is made, the settings variable will be checked immediately.
A quick sketch of the process may be:
my_instance = MySecretModel()
my_instance.save()
settings.DJANGO_ENCRYPTED_FIELD_KEY
.my_instance = MySecretModel.objects.get(id=1)
settings.DJANGO_ENCRYPTED_FIELD_KEY
.Some custom exceptions have been created to be able to differentiate from generic ones.
This exception will be raised when there is no DJANGO_ENCRYPTED_FIELD_KEY in settings.
This exception will be raised when DJANGO_ENCRYPTED_FIELD_KEY in settings is not bytes. Please, remember this key is bytes not string.
This exception will be raised when DJANGO_ENCRYPTED_FIELD_KEY in settings is has not the required length. Remember:
This exception will be raised when an unknown algorithm is passed to encrypt/decrypt.
This exception will be raised when an unknown AES algorithm is passed to encrypt/decrypt. Typically, an invalid mode within the AES algorithm.
FAQs
This is a Django Model Field class that can be encrypted using ChaCha20 poly 1305, and other algorithms.
We found that django-encrypted-field demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.