Skip to main content

Django NaCl Encrypted Fields

GitHub Actions: test GitHub Actions: lint Documentation Status

This is a collection of Django Model Field classes that are encrypted using PyNaCl. This package is largely based on django-encrypted-fields, which makes use of the outdated Keyczar library to encrypt fields. Besides that, it is inspired by django-fernet-field.

About PyNaCl

PyNaCl is a Python binding to libsodium, which is a fork of the Networking and Cryptography library. These libraries have a stated goal of improving usability, security and speed.

Getting Started

Install django-nacl-fields:

pip install django-nacl-fields

Add nacl_encrypted_fields to your INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'nacl_encrypted_fields'
    ...
]

Create a key to be used for encryption:

$ python manage.py createkey
# put the following line in your settings.py:
NACL_FIELDS_KEY = b'p1Et2Rb@;^BYdo`ZRFi!Hc-MXu(^|bVqA-FGqffM'

In your settings.py:

NACL_FIELDS_KEY = b'p1Et2Rb@;^BYdo`ZRFi!Hc-MXu(^|bVqA-FGqffM'

Then, in your models.py:

from django.db import models
from nacl_encrypted_fields.fields import NaClTextField


class MyModel(models.Model):
    text_field = NaClTextField()

Use the model as you would normally and the data will be stored encrypted in the database.

Note: Encrypted data cannot be used to query or sort. In SQL, these will all look like text fields with random text.

It is also possible to append the fields key to your settings file automatically upon creation, by using the -f flag:

python manage.py createkey -f settings.py

Where settings.py is the path to your settings file.

Available Fields

Currently build-in and unit-tested fields.

  • NaClCharField
  • NaClTextField
  • NaClDateTimeField
  • NaClIntegerField
  • NaClFloatField
  • NaClEmailField
  • NaClBooleanField
  • NaClJSONField

Encrypt Your Own Fields

Making new fields can be done by using the provided NaClFieldMixin:

from django.db import models
from nacl_encrypted_fields import NaClFieldMixin


class NaClIPAddressField(NaClFieldMixin, models.IPAddressField):
    pass

Please report any issues you encounter when trying this.

Contributing

Start a PostgreSQL Docker container for testing using:

docker run --rm -e POSTGRES_PASSWORD=postgres POSTGRES_DB=github_actions postgres

Then, you can run tests in another window using:

python -m venv env
source env/bin/activate
pip install '.[test]'
python manage.py test

Before you contribute, make sure you lint your code:

pip install '.[lint]'
flake8 --exclude .git,pycache,docs/conf.py,build,dist,env

After you code is done, free free to open a new pull request.

References

Metadata

Release files for django-nacl-fields 4.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-nacl-fields 4.1.0
File Size Uploaded
django-nacl-fields-4.1.0.tar.gz 14.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-nacl-fields 4.1.0
File Interpreter ABI Platform
django_nacl_fields-4.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 27.4 kB

Release files / django-nacl-fields-4.1.0.tar.gz

Download URL django-nacl-fields-4.1.0.tar.gz
Size 14.5 kB
Tags Source
SHA-256 checksum
How to use checksums
81e8426e0008ab94e98457677b68b6478b85d210a06c1e82de8c46dffcac5170
BLAKE2b-256 checksum
How to use checksums
9103c5ff45a1cb1bff266f7a58d72574059c32e8c49263963d1cbb5d60c49712
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / django_nacl_fields-4.1.0-py3-none-any.whl

Download URL django_nacl_fields-4.1.0-py3-none-any.whl
Size 12.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2412d4cec7c2034ffafa6a048a4ac5f0a81b401680b0ff4da801d32c2b159f28
BLAKE2b-256 checksum
How to use checksums
c79de25bb27b4bd84e38d3d99537548e724fe4d475ad12131fe18fdd21b2064b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

This release

4.1.0 This release

2 release files

4.0.0

2 release files

3.0.0

1 release file

2.0.0

1 release file

1.0.1

1 release file

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page