{"id":18898492,"url":"https://github.com/luojilab/django-mirage-field","last_synced_at":"2025-04-05T13:08:07.226Z","repository":{"id":47276289,"uuid":"183566951","full_name":"luojilab/django-mirage-field","owner":"luojilab","description":"Django model field encrypt/decrypt your data, keep secret in database.","archived":false,"fork":false,"pushed_at":"2022-04-19T11:01:05.000Z","size":80,"stargazers_count":91,"open_issues_count":1,"forks_count":14,"subscribers_count":15,"default_branch":"master","last_synced_at":"2024-04-29T14:44:52.140Z","etag":null,"topics":["aes","cbc","cipher","cryptography","django","encrypt","encryption","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/luojilab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-26T06:02:43.000Z","updated_at":"2024-04-29T11:04:32.000Z","dependencies_parsed_at":"2022-09-15T16:03:05.675Z","dependency_job_id":null,"html_url":"https://github.com/luojilab/django-mirage-field","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luojilab%2Fdjango-mirage-field","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luojilab%2Fdjango-mirage-field/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luojilab%2Fdjango-mirage-field/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luojilab%2Fdjango-mirage-field/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luojilab","download_url":"https://codeload.github.com/luojilab/django-mirage-field/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247339158,"owners_count":20923014,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aes","cbc","cipher","cryptography","django","encrypt","encryption","python"],"created_at":"2024-11-08T08:42:54.667Z","updated_at":"2025-04-05T13:08:07.210Z","avatar_url":"https://github.com/luojilab.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# django-mirage-field\n\n![](https://img.shields.io/pypi/v/django-mirage-field.svg?label=django-mirage-field)\n[![Hits](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2Fluojilab%2Fdjango-mirage-field\u0026count_bg=%233DC8BC\u0026title_bg=%23555555\u0026icon=\u0026icon_color=%23E7E7E7\u0026title=views\u0026edge_flat=false)](https://hits.seeyoufarm.com)\n\n## NOTE!!!\n\nThe maintainer [tcitry](https://github.com/tcitry) has resigned from luojilab, he won't contribute this project ever, the latest code please refer: [tcitry/django-mirage-field](https://github.com/tcitry/django-mirage-field).\n\n## Introduce\n\nA 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.\n\nMirage can also migrate data from origin column to encrypted column in database with a good performance.\n\n## Features\n\n* Use settings.SECRET_KEY as secret key default or anyelse which length \u003e= 32\n* Support CharField, TextField, IntegerField, EmailField\n* Support Django ORM's `get()`, `filter()` query method\n* Support AES-256-ECB and AES-256-CBC(v1.2.0)\n* Support PostgreSQL and MySQL database\n* Support Django model field `db_index` and `unique` attributes\n\n## Installation\n\n```bash\npip install django-mirage-field\n```\n\n## Usage\n\n```python\nfrom mirage import fields\nclass TestModel(models.Model):\n    age = fields.EncryptedIntegerField()\n```\n\n```python\nobj = TestModel.objects.get(age=18)\nobj.id          # 1\nobj.age         # 18\ntype(obj.age)   # int\n```\n\n```psql\ndatabase=# select * from testmodel where id = 1;\n         id          |           age\n---------------------+--------------------------\n 1 | -bYijegsEDrmS1s7ilnspA==\n```\n\n```python\nfrom mirage.crypto import Crypto\nc = Crypto()                      # key is optional, default will use settings.SECRET_KEY\nc.encrypt('some_address')               # -bYijegsEDrmS1s7ilnspA==\nc.decrypt('-bYijegsEDrmS1s7ilnspA==')   # some_address\n```\n\n## Settings\n\n- MIRAGE_SECRET_KEY\n- MIRAGE_CIPHER_MODE (v1.2.0+)\n- MIRAGE_CIPHER_IV (v1.2.0+)\n\n### MIRAGE_SECRET_KEY\n\nYou can use the `settings.SECRET_KEY` as default key, if you want custom another key for mirage, set the `MIRAGE_SECRET_KEY` in settings.\n\nMirage will get the `settings.MIRAGE_SECRET_KEY` first, if not set, mirage will get the `settings.SECRET_KEY`.\n\n### MIRAGE_CIPHER_MODE\n\n`MIRAGE_CIPHER_MODE` is optional, choices are below, If don't set, default is `ECB`.\n\n- `ECB`\n- `CBC`\n\n### MIRAGE_CIPHER_IV\n\n`MIRAGE_CIPHER_IV` is optional, if you don't set, it will use a default: \"1234567890abcdef\", it's length must be 16.\n\n\n## Model Fields\n\n1. EncryptedTextField\n2. EncryptedCharField\n3. EncryptedEmailField\n4. EncryptedIntegerField\n5. EncryptedURLField(v1.3.0+)\n\n## Data Migrate\n\nAdd`mirage`to`INSTALLED_APPS`\n\n### Way 1. Migrations\n\nadd `app_name`,`model_name`,`field_name` in [migrations.RunPython](https://docs.djangoproject.com/en/2.2/ref/migration-operations/#runpython)\n\n```\nfrom mirage.tools import Migrator\n\nmigrations.RunPython(Migrator(\"app_name\", \"model_name\", \"field_name\").encrypt, reverse_code=Migrator(\"app_name\", 'model_name', 'field_name').decrypt),\n```\n\n### Way 2. Commands\n\nOptions:\n\n* --app\n* --model\n* --field\n* --method (optional: `encrypt`, `decrypt`, `encrypt_to`, `decrypt_to`, `copy_to`)\n* --tofield (need when use `encryt_to`, `decrypt_to`, `copy_to` method)\n\nOptional options:\n\n* --offset (\"select * from xxx where id \u003e offset\")\n* --total (\"select * from xxx order by id limit total\")\n* --limit: set the query count in every update, default is 1000, if you set -1, mirage will query all rows one time to update.\n\nExamples\n\n```\n./manage.py mirage --app=yourapp --model=testmodel --field=address --method=encrypt --offset=2000000 --total=3000000\n\n./manage.py mirage --app=yourapp --model=testmodel --field=address --method=encrypt_to --tofield=encrypted_address\n\n```\n\n## Exceptions\n\n```\nfrom mirage import exceptions\n```\n\n1. EncryptedFieldException\n\n## Performance\n\n### With ECB mode\n\nMigrate data: 6000,000 columns takes 40 minutes, Average 1 column/2.5ms\n\nOnly encrypt/decrypt: Average 1 value/ms\n\n## Clients\n\n* [Java](https://github.com/luojilab/django-mirage-field/tree/master/client/java)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluojilab%2Fdjango-mirage-field","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluojilab%2Fdjango-mirage-field","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluojilab%2Fdjango-mirage-field/lists"}