Skip to main content

django-safe-fields

Save field value encrypted to database.

Install

pip install django-safe-fields

Shipped Fields

Mixins

  • SafeFieldMixinBase
  • SafeStringFieldMixin
  • SafeNumbericFieldMixinBase # used for fields that using none numberic database backend

Fields & Instance Extra Init Parameters (You can use django's fields default parameters)

  • SafeCharField
    • password: default to settings.SECRET_KEY.
    • cipher_class: choices are cipherutils.AesCipher, cipherutils.S12Cipher or something similar. default to cipherutils.AesCipher.
    • kwargs
    • cipher: or you can provides cipher instance instead of cipher_class and class parameters. Has higher priority than cipher_class.
  • SafeTextField
    • Same as SafeCharField
  • SafeEmailField
    • Same as SafeCharField
  • SafeURLField
    • Same as SafeCharField
  • SafeGenericIPAddressField
    • Same as SafeCharField
  • SafeIntegerField
    • Note: no extra init parameters
  • SafeBigIntegerField # using varchar(max_length=128) in datatabase storage
    • password
    • kwargs
      • int_digits: default to 12
  • SafeFloatField # using varchar(max_length=128) in database storage.
    • password
    • kwargs
      • int_digits: default to 12
      • float_digits: default to 4

Note

  1. Default cipher class is MysqlAesCipher. It keeps the same with mysql's aes_encrypt and aes_decrypt when the mysql's server variable block_encryption_mode=aes-128-ecb. The main trick is the method used to prepair the final key from the password.
  2. Default password is settings.SECRET_KEY, but we STRONGLY suggest you use different password for every different field.

Usage

pro/settings.py

INSTALLED_APPS = [
    ...
    'django_safe_fields',
    ...
]
  1. Insert django_safe_fields into INSTALLED_APPS.

app/models.py

from django.db import models
from django.conf import settings
from django_safe_fields.fields import SafeCharField
from django_safe_fields.fields import SafeGenericIPAddressField
from django_safe_fields.fields import SafeIntegerField
from fastutils.cipherutils import S12Cipher
from fastutils.cipherutils import HexlifyEncoder

class Account(models.Model):
    username = SafeCharField(max_length=64)
    name = SafeCharField(max_length=64, cipher_class=S12Cipher)
    email = SafeCharField(max_length=128, null=True, blank=True, cipher=S12Cipher(password=settings.SECRET_KEY, encoder=HexlifyEncoder(), force_text=True))
    last_login_ip = SafeGenericIPAddressField(max_length=256, null=True, blank=True, password="THIS FIELD PASSWORD")
    level = SafeIntegerField(null=True, blank=True)

    def __str__(self):
        return self.username

  1. All fields will be stored with encryption.
  2. Aes is a strong cipher.
  3. With aes encryption, you can NOT search partly, only the exact search rule will be accepted.
  4. With aes encryption, you can NOT sort.
  5. S12Cipher is string encode method that keeps the sorting result after encoded.
  6. IvCihper is a week cipher for integer field that let you sort with the field.

Test Passed On Python and Django Versions

  • python27:~=django1.11.29
  • python34:~=django1.11.29
  • python34:~=django2.0.13
  • python35:~=django1.11.29
  • python35:~=django2.0.13
  • python35:~=django2.1.15
  • python35:~=django2.2.28
  • python36:~=django2.0.13
  • python36:~=django2.1.15
  • python36:~=django2.2.28
  • python36:~=django3.0.14
  • python36:~=django3.1.14
  • python36:~=django3.2.21
  • python37:~=django2.0.13
  • python37:~=django2.1.15
  • python37:~=django2.2.28
  • python37:~=django3.0.14
  • python37:~=django3.1.14
  • python37:~=django3.2.21
  • python38:~=django2.0.13
  • python38:~=django2.1.15
  • python38:~=django2.2.28
  • python38:~=django3.0.14
  • python38:~=django3.1.14
  • python38:~=django3.2.21
  • python38:~=django4.0.10
  • python38:~=django4.1.11
  • python38:~=django4.2.5
  • python39:~=django2.0.13
  • python39:~=django2.1.15
  • python39:~=django2.2.28
  • python39:~=django3.0.14
  • python39:~=django3.1.14
  • python39:~=django3.2.21
  • python39:~=django4.0.10
  • python39:~=django4.1.11
  • python39:~=django4.2.5
  • python310:~=django2.1.15
  • python310:~=django2.2.28
  • python310:~=django3.0.14
  • python310:~=django3.1.14
  • python310:~=django3.2.21
  • python310:~=django4.0.10
  • python310:~=django4.1.11
  • python310:~=django4.2.5
  • python311:~=django2.2.28
  • python311:~=django3.0.14
  • python311:~=django3.1.14
  • python311:~=django3.2.21
  • python311:~=django4.0.10
  • python311:~=django4.1.11
  • python311:~=django4.2.5

Releases

v0.2.3

  • 修正:mapping_cipher_fields_dumps与最新版本zenutils匹配的问题。

v0.2.2

  • Fix fastutils.strutils.force_text problem. Use zenutils.sixutils.TEXT instead.

v0.2.1

  • Fix problem with latest version of fastutils.

v0.1.11

  • Fix callable default value problem.

v0.1.7

  • Add used_ciphers parameters support, so that we can decrypt old data when we change cipher_class or field password.
  • Add safe field management commands: list_safe_fields, mapping_cipher_fields_dumps. Note: Use mapping_cipher_fields_dumps to speed up the safe field initialization.

v0.1.6

  • Fix xxx__in query problem.

v0.1.5

  • Turn to bytes before doing encryption.

v0.1.4

  • Change init parameter encoder to result_encoder.

v0.1.3

  • Fix get_db_prep_lookup problem.

v0.1.2

  • Add SafeBigIntegerField and SafeFloatField.

v0.1.1

  • Fix problem in objects.get that double encrypt the raw data.

v0.1.0

  • First release.

Release files for django-safe-fields 0.2.3

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-safe-fields 0.2.3
File Size Uploaded
django-safe-fields-0.2.3.tar.gz 12.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-safe-fields 0.2.3
File Interpreter ABI Platform
django_safe_fields-0.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 23.6 kB

Release files / django-safe-fields-0.2.3.tar.gz

Download URL django-safe-fields-0.2.3.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
9bb70282d6f7e3fc543835c0d82e5b59189cdab903f7ba6fd0a5b2044b2ee66b
BLAKE2b-256 checksum
How to use checksums
7082478d21f205a202bd06a798c9dc69d2afb22e0fef6b954659132e39b7ecc5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release files / django_safe_fields-0.2.3-py3-none-any.whl

Download URL django_safe_fields-0.2.3-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
34123eb329c9e3f19c266b364f5abc6f4a04396e7b34199eda4c9b36efd7f5ab
BLAKE2b-256 checksum
How to use checksums
63a4d3f4d430426245ea3536e75244accb66ca216d2c78f41db849e06736ce9b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release history Release notifications | RSS feed

This release

0.2.3 This release

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.1.11

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

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