Skip to main content

Django model fields that store the values encrypted using Fernet.

Project description

Django Fernet

PyPI Test Status Codecov

A library that provides Django model fields to store value encrypted using Fernet.

Installation

With a correctly configured pipenv toolchain:

pipenv install django-fernet

You may also use classic pip to install the package:

pip install django-fernet

Auto-formatter setup

We use ruff (https://github.com/astral-sh/ruff) for local auto-formatting and for linting in the CI pipeline. The pre-commit framework (https://pre-commit.com) provides Git hooks for these tools, so they are automatically applied before every commit.

Steps to activate:

  • Install the pre-commit framework: pip install pre-commit (for alternative installation options see https://pre-commit.com/#install)
  • Activate the framework (from the root directory of the repository): pre-commit install

Hint: You can also run the formatters manually at any time with the following command: pre-commit run --all-files

Getting started

Example model that defines fernet text fields

from django.db import models
from django_fernet.fields import *


class ExampleTextModel(models.Model):
    example_field = FernetTextField(
        verbose_name="Example field",
    )

Example model that defines fernet binary fields

from django.db import models
from django_fernet.fields import *


class ExampleBinaryModel(models.Model):
    example_field = FernetBinaryField(
        verbose_name="Example field",
    )

How to use

Save encrypted text to the database

from django_fernet.fernet import *

field_data = FernetTextFieldData()
field_data.encrypt("foo", "--secret--")

instance = ExampleTextModel()
instance.example_field = field_data
instance.save()

Save encrypted binary data to the database

from django_fernet.fernet import *

field_data = FernetTextFieldData()
field_data.encrypt(b"foo", "--secret--")

instance = ExampleBinaryModel()
instance.example_field = field_data
instance.save()

Load encrypted text from the database

instance = ExampleTextModel.objects.get(pk=...)
decrypted_str = instance.example_field.decrypt("--secret--")

Load encrypted binary data from the database

instance = ExampleBinaryModel.objects.get(pk=...)
decrypted_bytes = instance.example_field.decrypt("--secret--")

Supported versions

Django 4.2 Django 5.0 Django 5.1
Python 3.10
Python 3.11
Python 3.12
Python 3.13
PyPy 3.10

Tests

An example Django app that makes use of django-fernet can be found in the tests/ folder. This example Django app also contains the unit tests.

Follow below instructions to run the tests. You may exchange the installed Django version according to your requirements.

# install dependencies
python -m pip install --upgrade pip
pip install -r requirements.txt

# run tests
cd tests && pytest

List of developers

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-fernet-1.0.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

django_fernet-1.0.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file django-fernet-1.0.1.tar.gz.

File metadata

  • Download URL: django-fernet-1.0.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.15

File hashes

Hashes for django-fernet-1.0.1.tar.gz
Algorithm Hash digest
SHA256 5efd93818c10eb2c22ddb70feb01827af5e158eb0d9ced495bf99f309ba5e3ef
MD5 7ef78762d13620023dfa4483b2d56dc9
BLAKE2b-256 5ec92b27096ca1cd129de03c82c4a625f291800a42c0862cda6402c690129577

See more details on using hashes here.

File details

Details for the file django_fernet-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_fernet-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 26db0c46a53d0eac6332de8862d740ef9e7638d4410ff3615631ce1cbbc751e9
MD5 05e9d364081e33dfbe3e49cafded86e7
BLAKE2b-256 fed0265ce523f743c7d40e9e67f955f954c2410d39c26478af54f38f8186f9ca

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page