django-safe-fields


Namedjango-safe-fields JSON
Version 0.2.2 PyPI version JSON
download
home_page
SummarySave field value encrypted to database.
upload_time2023-09-09 08:59:55
maintainerZhou Zhao
docs_urlNone
authorZhou Zhao
requires_python
licenseMIT
keywords django extentions
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # django-safe-fields

Save field value encrypted to database.

## Install

```shell
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
        - **Note**: kwargs parameters depend on the cipher class you choose. see details at https://pypi.org/project/fastutils/.
    - 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.
1. 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.
1. Aes is a strong cipher.
1. With aes encryption, you can NOT search partly, only the `exact` search rule will be accepted.
1. With aes encryption, you can NOT sort.
1. S12Cipher is string encode method that keeps the sorting result after encoded.
1. 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.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.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "django-safe-fields",
    "maintainer": "Zhou Zhao",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "zhouzhao@zencore.cn",
    "keywords": "django extentions",
    "author": "Zhou Zhao",
    "author_email": "zhouzhao@zencore.cn",
    "download_url": "https://files.pythonhosted.org/packages/06/ad/0bd0387803e2133c05a36a58426fa26a08165971c4bda21a5cf833b6e732/django-safe-fields-0.2.2.tar.gz",
    "platform": null,
    "description": "# django-safe-fields\n\nSave field value encrypted to database.\n\n## Install\n\n```shell\npip install django-safe-fields\n```\n\n## Shipped Fields\n\n**Mixins**\n\n- SafeFieldMixinBase\n- SafeStringFieldMixin\n- SafeNumbericFieldMixinBase # used for fields that using none numberic database backend\n\n**Fields & Instance Extra Init Parameters (You can use django's fields default parameters)**\n\n- SafeCharField\n    - password: default to settings.SECRET_KEY.\n    - cipher_class: choices are cipherutils.AesCipher, cipherutils.S12Cipher or something similar. default to cipherutils.AesCipher.\n    - kwargs\n        - **Note**: kwargs parameters depend on the cipher class you choose. see details at https://pypi.org/project/fastutils/.\n    - cipher: or you can provides cipher instance instead of cipher_class and class parameters. Has higher priority than cipher_class.\n- SafeTextField\n    - Same as SafeCharField\n- SafeEmailField\n    - Same as SafeCharField\n- SafeURLField\n    - Same as SafeCharField\n- SafeGenericIPAddressField\n    - Same as SafeCharField\n- SafeIntegerField\n    - **Note**: no extra init parameters\n- SafeBigIntegerField # using varchar(max_length=128) in datatabase storage\n    - password\n    - kwargs\n        - int_digits: default to 12\n- SafeFloatField # using varchar(max_length=128) in database storage.\n    - password\n    - kwargs\n        - int_digits: default to 12\n        - float_digits: default to 4\n\n## Note\n\n1. 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.\n1. Default password is settings.SECRET_KEY, but we STRONGLY suggest you use different password for every different field.\n\n## Usage\n\n**pro/settings.py**\n\n```\nINSTALLED_APPS = [\n    ...\n    'django_safe_fields',\n    ...\n]\n```\n\n1. Insert `django_safe_fields` into INSTALLED_APPS.\n\n**app/models.py**\n\n```\nfrom django.db import models\nfrom django.conf import settings\nfrom django_safe_fields.fields import SafeCharField\nfrom django_safe_fields.fields import SafeGenericIPAddressField\nfrom django_safe_fields.fields import SafeIntegerField\nfrom fastutils.cipherutils import S12Cipher\nfrom fastutils.cipherutils import HexlifyEncoder\n\nclass Account(models.Model):\n    username = SafeCharField(max_length=64)\n    name = SafeCharField(max_length=64, cipher_class=S12Cipher)\n    email = SafeCharField(max_length=128, null=True, blank=True, cipher=S12Cipher(password=settings.SECRET_KEY, encoder=HexlifyEncoder(), force_text=True))\n    last_login_ip = SafeGenericIPAddressField(max_length=256, null=True, blank=True, password=\"THIS FIELD PASSWORD\")\n    level = SafeIntegerField(null=True, blank=True)\n\n    def __str__(self):\n        return self.username\n\n```\n\n1. All fields will be stored with encryption.\n1. Aes is a strong cipher.\n1. With aes encryption, you can NOT search partly, only the `exact` search rule will be accepted.\n1. With aes encryption, you can NOT sort.\n1. S12Cipher is string encode method that keeps the sorting result after encoded.\n1. IvCihper is a week cipher for integer field that let you sort with the field.\n\n## Test Passed On Python and Django Versions\n\n- python27:~=django1.11.29\n- python34:~=django1.11.29\n- python34:~=django2.0.13\n- python35:~=django1.11.29\n- python35:~=django2.0.13\n- python35:~=django2.1.15\n- python35:~=django2.2.28\n- python36:~=django2.0.13\n- python36:~=django2.1.15\n- python36:~=django2.2.28\n- python36:~=django3.0.14\n- python36:~=django3.1.14\n- python36:~=django3.2.21\n- python37:~=django2.0.13\n- python37:~=django2.1.15\n- python37:~=django2.2.28\n- python37:~=django3.0.14\n- python37:~=django3.1.14\n- python37:~=django3.2.21\n- python38:~=django2.0.13\n- python38:~=django2.1.15\n- python38:~=django2.2.28\n- python38:~=django3.0.14\n- python38:~=django3.1.14\n- python38:~=django3.2.21\n- python38:~=django4.0.10\n- python38:~=django4.1.11\n- python38:~=django4.2.5\n- python39:~=django2.0.13\n- python39:~=django2.1.15\n- python39:~=django2.2.28\n- python39:~=django3.0.14\n- python39:~=django3.1.14\n- python39:~=django3.2.21\n- python39:~=django4.0.10\n- python39:~=django4.1.11\n- python39:~=django4.2.5\n- python310:~=django2.1.15\n- python310:~=django2.2.28\n- python310:~=django3.0.14\n- python310:~=django3.1.14\n- python310:~=django3.2.21\n- python310:~=django4.0.10\n- python310:~=django4.1.11\n- python310:~=django4.2.5\n- python311:~=django2.2.28\n- python311:~=django3.0.14\n- python311:~=django3.1.14\n- python311:~=django3.2.21\n- python311:~=django4.0.10\n- python311:~=django4.1.11\n- python311:~=django4.2.5\n\n## Releases\n\n### v0.2.2\n\n- Fix fastutils.strutils.force_text problem. Use zenutils.sixutils.TEXT instead.\n\n### v0.2.1\n\n- Fix problem with latest version of fastutils.\n\n### v0.1.11\n\n- Fix callable default value problem.\n\n### v0.1.7\n\n- Add used_ciphers parameters support, so that we can decrypt old data when we change cipher_class or field password.\n- 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.\n\n### v0.1.6\n\n- Fix xxx__in query problem.\n\n### v0.1.5\n\n- Turn to bytes before doing encryption.\n\n### v0.1.4\n\n- Change init parameter encoder to result_encoder.\n\n### v0.1.3\n\n- Fix get_db_prep_lookup problem.\n\n### v0.1.2\n\n- Add SafeBigIntegerField and SafeFloatField.\n\n### v0.1.1\n\n- Fix problem in objects.get that double encrypt the raw data.\n\n### v0.1.0\n\n- First release.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Save field value encrypted to database.",
    "version": "0.2.2",
    "project_urls": null,
    "split_keywords": [
        "django",
        "extentions"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "52b9b2d5a9d0f1d5d02dc3d8a8038ea83f287aa011879832dc7c8083ce62138d",
                "md5": "f9441408ce34b845e7cc98642b5915d6",
                "sha256": "61a72fa1ea7f6566258fbee6d111cd53397592d2e51b6f1240e1ad4e60bbc603"
            },
            "downloads": -1,
            "filename": "django_safe_fields-0.2.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f9441408ce34b845e7cc98642b5915d6",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 7839,
            "upload_time": "2023-09-09T08:59:53",
            "upload_time_iso_8601": "2023-09-09T08:59:53.054183Z",
            "url": "https://files.pythonhosted.org/packages/52/b9/b2d5a9d0f1d5d02dc3d8a8038ea83f287aa011879832dc7c8083ce62138d/django_safe_fields-0.2.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "06ad0bd0387803e2133c05a36a58426fa26a08165971c4bda21a5cf833b6e732",
                "md5": "2d7da952849fdf1d20ff6be08a2bafac",
                "sha256": "094015e6f434a93f6c6ab7871d50c088d9846186f14a5c400e3b1da704209268"
            },
            "downloads": -1,
            "filename": "django-safe-fields-0.2.2.tar.gz",
            "has_sig": false,
            "md5_digest": "2d7da952849fdf1d20ff6be08a2bafac",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 8506,
            "upload_time": "2023-09-09T08:59:55",
            "upload_time_iso_8601": "2023-09-09T08:59:55.293738Z",
            "url": "https://files.pythonhosted.org/packages/06/ad/0bd0387803e2133c05a36a58426fa26a08165971c4bda21a5cf833b6e732/django-safe-fields-0.2.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-09 08:59:55",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "django-safe-fields"
}
        
Elapsed time: 0.11015s