django-mirage-field
Introduce
A Django model fields collection that encrypt your data when save to and decrypt when get from database. It keeps data always encrypted in database. Base on AES, it supports query method like get() and filter() in Django.
Mirage can also migrate data from origin column to encrypted column in database with a good performance.
Features
- Use settings.SECRET_KEY as secret key default or anyelse which length >= 32
- Support CharField, TextField, IntegerField, EmailField
- Support Django ORM's
get(),filter()query method - Support AES-256-ECB and AES-256-CBC(v1.2.0)
- Support PostgreSQL and MySQL database
- Support Django model field
db_indexanduniqueattributes
Installation
pip install django-mirage-field
Usage
from mirage import fields
class TestModel(models.Model):
age = fields.EncryptedIntegerField()
obj = TestModel.objects.get(age=18)
obj.id # 1
obj.age # 18
type(obj.age) # int
database=# select * from testmodel where id = 1;
id | age
---------------------+--------------------------
1 | -bYijegsEDrmS1s7ilnspA==
from mirage.crypto import Crypto
c = Crypto() # key is optional, default will use settings.SECRET_KEY
c.encrypt('some_address') # -bYijegsEDrmS1s7ilnspA==
c.decrypt('-bYijegsEDrmS1s7ilnspA==') # some_address
Settings
- MIRAGE_SECRET_KEY
- MIRAGE_CIPHER_MODE (v1.2.0+)
- MIRAGE_CIPHER_IV (v1.2.0+)
MIRAGE_SECRET_KEY
You can use the settings.SECRET_KEY as default key, if you want custom another key for mirage, set the MIRAGE_SECRET_KEY in settings.
Mirage will get the settings.MIRAGE_SECRET_KEY first, if not set, mirage will get the settings.SECRET_KEY.
MIRAGE_CIPHER_MODE
MIRAGE_CIPHER_MODE is optional, choices are below, If don't set, default is ECB.
ECBCBC
MIRAGE_CIPHER_IV
MIRAGE_CIPHER_IV is optional, if you don't set, it will use a default: "1234567890abcdef", it's length must be 16.
Model Fields
- EncryptedTextField
- EncryptedCharField
- EncryptedEmailField
- EncryptedIntegerField
- EncryptedURLField(v1.3.0+)
Data Migrate
AddmiragetoINSTALLED_APPS
Way 1. Migrations
add app_name,model_name,field_name in migrations.RunPython
from mirage.tools import Migrator
migrations.RunPython(Migrator("app_name", "model_name", "field_name").encrypt, reverse_code=Migrator("app_name", 'model_name', 'field_name').decrypt),
Way 2. Commands
Options:
- --app
- --model
- --field
- --method (optional:
encrypt,decrypt,encrypt_to,decrypt_to,copy_to) - --tofield (need when use
encryt_to,decrypt_to,copy_tomethod)
Optional options:
- --offset ("select * from xxx where id > offset")
- --total ("select * from xxx order by id limit total")
- --limit: set the query count in every update, default is 1000, if you set -1, mirage will query all rows one time to update.
Examples
./manage.py mirage --app=yourapp --model=testmodel --field=address --method=encrypt --offset=2000000 --total=3000000
./manage.py mirage --app=yourapp --model=testmodel --field=address --method=encrypt_to --tofield=encrypted_address
Exceptions
from mirage import exceptions
- EncryptedFieldException
Performance
With ECB mode
Migrate data: 6000,000 columns takes 40 minutes, Average 1 column/2.5ms
Only encrypt/decrypt: Average 1 value/ms
Clients
Metadata
Release files for django-mirage-field 1.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| django-mirage-field-1.4.0.tar.gz | 7.4 kB | Without provenance | Details |
Release files / django-mirage-field-1.4.0.tar.gz
| Download URL | django-mirage-field-1.4.0.tar.gz |
|---|---|
| Size | 7.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e768c8353f9d8fd34933e8fda60237f49c4b3456e68f1fea2b0f49e3381f3386
|
|
BLAKE2b-256 checksum How to use checksums |
7eca965987d85817039d168270f59aaf55166499496fe1233dafdd84965ae3d2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9
|
Security for django-mirage-field 1.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.