A Django model field that encrypt your data when save to and decrypt when get from database. It keeps data always encrypted in database.
Project description
django-mirage-field
Introduce
A Django model field that encrypt your data when save to and decrypt when get from database. It keeps data always encrypted in database. Base on symmetric encryption, it support query the origin text and return encrypted objects in Django.
Support
- 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 - Use AES-256-ECB algorithm
- Support PostgreSQL and MySQL database
- Support Django model field
db_index
andunique
attributes
Installation
pip install django-mirage-field
Usage
from mirage import fields
class TestModel(models.Model):
phone = fields.EncryptedIntegerField()
obj = TestModel.objects.get(phone=18866677777)
obj.id # 123
obj.phone # 18866677777
type(obj.phone) # int
database=# select * from testmodel where id = 123;
id | phone
---------------------+--------------------------
123 | -bYijegsEDrmS1s7ilnspA==
from mirage.crypto import Crypto
c = Crypto(key="") # key is optional, default will use settings.SECRET_KEY
c.encrypt('18866677777') # -bYijegsEDrmS1s7ilnspA==
c.decrypt('-bYijegsEDrmS1s7ilnspA==') # 18866677777
Model Fields
- EncryptedTextField
- EncryptedCharField
- EncryptedEmailField
- EncryptedIntegerField
Data Migrate
Addmirage
toINSTALLED_APPS
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),
2. Commands
Options:
- --app
- --model
- --field
- --method (optional:
encrypt
,decrypt
,encrypt_to
,decrypt_to
,copy
) - --tofield (need when use
encryt_to
,decrypt_to
,copy
method)
Optional options:
- --offset ("select * from xxx where id > offset")
- --total ("select * from xxx order by id limit total")
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
Migrate data: 6000,000 columns takes 40 minutes, Average 1 column/2.5ms
Only encrypt/decrypt: Average 1 value/ms
Clients
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Close
Hashes for django-mirage-field-0.0.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8aaa459f5c895abb318daae0e9f147a20a7c5013ec67198c2680d07cc904a341 |
|
MD5 | 22656b0ac2c31603c7b07cd77023c794 |
|
BLAKE2b-256 | f982a8ab839bac9d87c07594c1e56fd2dbc1221af998e0296c53d0d3d413c7c0 |