Skip to content

incuna/django-field-cryptography

Repository files navigation

Django Field Cryptography Build Status


Warning if you have used django-simple-aes-field

django-simple-aes-field can be found under the tag v0.1.3.

This project was formely known as django-simple-aes-field which has been deprecated.

To migrate from django-simple-aes-field to django-field-cryptography:

  • create a model with the new fields;
  • create a migration to transfer the data;
  • remove the old fields.

django-field-cryptography provides a django TextField subclass which encodes itself using cryptography in the database.

EncryptedTextField uses Fernet which is an implementation of symmetric cryptography.

Fernet uses AES in CBC mode and HMAC for authentication. See implementation for more details.

Installation

Install with your favourite package manager:

    pip install django-field-cryptography

Add FERNET_KEY to your settings.py. A FERNET_KEY should contains 32 characters and should be encoded with base64. It's possible to generate one with the cryptography library.

To generate a key:

>>> from cryptography.fernet import Fernet
>>> Fernet.generate_key()
b'your_key...'
>>>

In settings.py:

FERNET_KEY = b'your_key...'

Usage

    from django_cryptography.fields import EncryptedTextField

    class MyModel(models.Model):
        my_secret_field = EncryptedTextField()