Skip to main content

No project description provided

Project description

Django Secured Fields

GitHub GitHub Workflow Status codecov PyPI
PyPI - Python Version Django Version PostgreSQL Version MySQL Version

Django encrypted fields with search enabled.


  • Automatically encrypt/decrypt field value using cryptography's Fernet
  • Built-in search lookup on the encrypted fields from hashlib's SHA-256 hash value. in lookup also supported.
  • Supports most of available Django fields including BinaryField, JSONField, and FileField.


pip install django-secured-fields


  1. Add secured_fields into INSTALLED_APPS

  2. Generate a new key using for encryption

    $ python generate_key
    KEY: TtY8MAeXuhdKDd1HfGUwim-vQ8H7fXyRQ9J8pTi_-lg=
    HASH_SALT: 500d492e
  3. Put generated key and hash salt in settings

    SECURED_FIELDS_KEY = 'TtY8MAeXuhdKDd1HfGUwim-vQ8H7fXyRQ9J8pTi_-lg='
    SECURED_FIELDS_HASH_SALT = '500d492e'  # optional


Simple Usage

import secured_fields

phone_number = secured_fields.EncryptedCharField(max_length=10)

Enable Searching

import secured_fields

id_card_number = secured_fields.EncryptedCharField(max_length=18, searchable=True)

Supported Fields

  • EncryptedBinaryField
  • EncryptedBooleanField
  • EncryptedCharField
  • EncryptedDateField
  • EncryptedDateTimeField
  • EncryptedDecimalField
  • EncryptedFileField
  • EncryptedImageField
  • EncryptedIntegerField
  • EncryptedJSONField
  • EncryptedTextField


Key Required Default Description
SECURED_FIELDS_KEY Yes Key for using in encryption/decryption with Fernet. Usually generated from python generate_key.
SECURED_FIELDS_HASH_SALT No '' Salt to append after the field value before hashing. Usually generated from python generate_key.
SECURED_FIELDS_FILE_STORAGE No '' File storage class used for storing encrypted file/image fields. See EncryptedStorageMixin


Field Arguments

Name Type Required Default Description
searchable bool No False Enable search function


> from secured_fields.fernet import get_fernet

> data = b'test'

> encrypted_data = get_fernet().encrypt(data)
> encrypted_data

> get_fernet().decrypt(encrypted_data)


If you have a field which does not supported by the package, you can use EncryptedMixin to enable encryption and search functionality for that custom field.

import secured_fields
from django.db import models

class EncryptedUUIDField(secured_fields.EncryptedMixin, models.UUIDField):

task_id = EncryptedUUIDField(searchable=True)


If you use a custom file storage class (e.g. defined in's DEFAULT_FILE_STORAGE), you can enable file encryption using EncryptedStorageMixin.

import secured_fields
from import MinioMediaStorage

class EncryptedMinioMediaStorage(

Known Limitation

  • in lookup on JSONField is not available
  • Large files are not performance-friendly at the moment (see #2)
  • Search on BinaryField does not supported at the moment (see #6)
  • Changing searchable value in a field with the records in the database is not supported (see #7)



  • Docker
  • Poetry
  • MySQL Client
    • brew install mysql-client
    • echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.bash_profile

Running Project

  1. Start backend databases

    make up-db
  2. Run tests (see: Testing)


make lint


make test

Fix Formatting

make yapf

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-secured-fields-0.3.0.tar.gz (9.7 kB view hashes)

Uploaded Source

Built Distribution

django_secured_fields-0.3.0-py3-none-any.whl (9.7 kB view hashes)

Uploaded Python 3

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