# ORM Encrypt Decrypt Fields
A Django and SQLAlchemy model field that encrypts your data based SHA256 algorithm and Fernet (symmetric encryption)
when saving to the model field. The fernet module guarantees that data encrypted using it cannot be further manipulated
or read without the key. It keeps data always encrypted in the database.
Also, possible to use it directly with the Crypto class.
[![ProjectCheck](https://github.com/alpden550/encrypt-decrypt-fields/actions/workflows/check.yml/badge.svg)](https://github.com/alpden550/encrypt-decrypt-fields/actions/workflows/check.yml)
## How install
```
pip install encrypt-decrypt-fields
```
## Usage
For Django use project secret key or own:
```python
from django.db import models
from encrypt_decrypt_fields import EncryptedBinaryField
class DemoModel(models.Model):
password = EncryptedBinaryField(blank=True, null=True)
```
```python
from .models import DemoModel
DemoModel.objects.create(password='password')
demo = DemoModel.objects.get(id=1)
print(demo.password.to_bytes())
# b'gAAAAABgxGVVeTPV9i1nPNl91Ss4XVH0rD6eJCgOWIOeRwtagp12gBJg9DL_HXODTDW0WKsqc8Z9vsuHUiAr3qQVE9YQmTd3pg=='
```
To read bytes in postgres, use to_bytes() method of memoryview
```
obj.password.to_bytes()
```
or
```
bytes(obj.password, 'utf-8')
```
To decrypt value use Crypto class:
```python
from django.conf import settings
from encrypt_decrypt_fields import Crypto
from .models import DemoModel
obj = DemoModel.objects.get(id=1)
decrypted = Crypto(settings.SECRET_KEY).decrypt_token(obj.password.to_bytes())
print(decrypted)
# 'password'
```
For SQLAlchemy, it is similar:
```python
from sqlalchemy import Column, Integer, String
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base, sessionmaker
from encrypt_decrypt_fields import Crypto, EncryptedAlchemyBinaryField
Base = declarative_base()
engine = create_engine("sqlite:///:memory:", echo=True)
class Demo(Base):
__tablename__ = 'demo'
id = Column(Integer, primary_key=True)
name = Column(String)
password = Column(EncryptedAlchemyBinaryField(key='secret'), nullable=True)
Session = sessionmaker(bind=engine)
session = Session()
demo = session.query(Demo).first()
Crypto('secret').decrypt_token(demo.password)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/alpden550/django-encrypt-decrypt",
"name": "encrypt-decrypt-fields",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.10,<4.0",
"maintainer_email": "",
"keywords": "django,django-orm,cryptography,development,orm,sqlachemy-orm",
"author": "Denis Novikov",
"author_email": "alpden550@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/68/57/1e99f55679bd6f603a22c7efa8e104ba92d052f349ad0b30a8b354a7c0b9/encrypt_decrypt_fields-1.3.6.tar.gz",
"platform": null,
"description": "# ORM Encrypt Decrypt Fields\n\nA Django and SQLAlchemy model field that encrypts your data based SHA256 algorithm and Fernet (symmetric encryption)\nwhen saving to the model field. The fernet module guarantees that data encrypted using it cannot be further manipulated\nor read without the key. It keeps data always encrypted in the database.\n\nAlso, possible to use it directly with the Crypto class.\n\n[![ProjectCheck](https://github.com/alpden550/encrypt-decrypt-fields/actions/workflows/check.yml/badge.svg)](https://github.com/alpden550/encrypt-decrypt-fields/actions/workflows/check.yml)\n\n## How install\n\n```\npip install encrypt-decrypt-fields\n```\n\n## Usage\n\nFor Django use project secret key or own:\n\n```python\nfrom django.db import models\nfrom encrypt_decrypt_fields import EncryptedBinaryField\n\n\nclass DemoModel(models.Model):\n password = EncryptedBinaryField(blank=True, null=True)\n```\n\n```python\nfrom .models import DemoModel\n\nDemoModel.objects.create(password='password')\n\ndemo = DemoModel.objects.get(id=1)\nprint(demo.password.to_bytes()) \n# b'gAAAAABgxGVVeTPV9i1nPNl91Ss4XVH0rD6eJCgOWIOeRwtagp12gBJg9DL_HXODTDW0WKsqc8Z9vsuHUiAr3qQVE9YQmTd3pg=='\n```\n\nTo read bytes in postgres, use to_bytes() method of memoryview\n\n```\nobj.password.to_bytes()\n```\n\nor\n\n```\nbytes(obj.password, 'utf-8')\n```\n\nTo decrypt value use Crypto class:\n\n```python\nfrom django.conf import settings\nfrom encrypt_decrypt_fields import Crypto\nfrom .models import DemoModel\n\n\nobj = DemoModel.objects.get(id=1)\n\ndecrypted = Crypto(settings.SECRET_KEY).decrypt_token(obj.password.to_bytes())\nprint(decrypted) \n# 'password'\n```\n\nFor SQLAlchemy, it is similar:\n\n```python\nfrom sqlalchemy import Column, Integer, String\nfrom sqlalchemy import create_engine\nfrom sqlalchemy.orm import declarative_base, sessionmaker\n\nfrom encrypt_decrypt_fields import Crypto, EncryptedAlchemyBinaryField\n\nBase = declarative_base()\nengine = create_engine(\"sqlite:///:memory:\", echo=True)\n\n\nclass Demo(Base):\n __tablename__ = 'demo'\n\n id = Column(Integer, primary_key=True)\n name = Column(String)\n password = Column(EncryptedAlchemyBinaryField(key='secret'), nullable=True)\n\n\nSession = sessionmaker(bind=engine)\nsession = Session()\n\ndemo = session.query(Demo).first()\nCrypto('secret').decrypt_token(demo.password) \n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Encrypt and decrypt fields for a Django, SQLAlchemy ORM models or for direct use Crypto class",
"version": "1.3.6",
"project_urls": {
"Homepage": "https://github.com/alpden550/django-encrypt-decrypt",
"Repository": "https://github.com/alpden550/django-encrypt-decrypt"
},
"split_keywords": [
"django",
"django-orm",
"cryptography",
"development",
"orm",
"sqlachemy-orm"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "57d947fcf907c2d196b1feea2430f8b4f3ac12c6cff8c3873ff754b5cfad4bdc",
"md5": "9c92d3e61dd0251e396a484e25aed528",
"sha256": "2ef69f9e6182c44e12a5b87abb3e8683566ff97bbc2aa8c398bef9393b20b87f"
},
"downloads": -1,
"filename": "encrypt_decrypt_fields-1.3.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9c92d3e61dd0251e396a484e25aed528",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10,<4.0",
"size": 7104,
"upload_time": "2023-10-31T08:24:25",
"upload_time_iso_8601": "2023-10-31T08:24:25.547497Z",
"url": "https://files.pythonhosted.org/packages/57/d9/47fcf907c2d196b1feea2430f8b4f3ac12c6cff8c3873ff754b5cfad4bdc/encrypt_decrypt_fields-1.3.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "68571e99f55679bd6f603a22c7efa8e104ba92d052f349ad0b30a8b354a7c0b9",
"md5": "18739473fb6623189eb470ba835a353f",
"sha256": "ce2834d0da1a61868cececca20796ee261f379f8aaee786186ff0fe1cd8220f1"
},
"downloads": -1,
"filename": "encrypt_decrypt_fields-1.3.6.tar.gz",
"has_sig": false,
"md5_digest": "18739473fb6623189eb470ba835a353f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10,<4.0",
"size": 4704,
"upload_time": "2023-10-31T08:24:26",
"upload_time_iso_8601": "2023-10-31T08:24:26.992996Z",
"url": "https://files.pythonhosted.org/packages/68/57/1e99f55679bd6f603a22c7efa8e104ba92d052f349ad0b30a8b354a7c0b9/encrypt_decrypt_fields-1.3.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-31 08:24:26",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "alpden550",
"github_project": "django-encrypt-decrypt",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "encrypt-decrypt-fields"
}