https://github.com/alpden550/encrypt-decrypt-fields
Encrypt and decrypt fields for Django and SQLAlcemy.
https://github.com/alpden550/encrypt-decrypt-fields
cryptography django django-orm encrypts fernet orm python3 sqlalchemy
Last synced: about 1 month ago
JSON representation
Encrypt and decrypt fields for Django and SQLAlcemy.
- Host: GitHub
- URL: https://github.com/alpden550/encrypt-decrypt-fields
- Owner: alpden550
- License: mit
- Created: 2021-06-10T08:45:42.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-04-15T22:53:34.000Z (about 2 years ago)
- Last Synced: 2026-01-13T19:52:15.740Z (3 months ago)
- Topics: cryptography, django, django-orm, encrypts, fernet, orm, python3, sqlalchemy
- Language: Python
- Homepage:
- Size: 262 KB
- Stars: 11
- Watchers: 1
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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.
[](https://github.com/alpden550/encrypt-decrypt-fields/actions/workflows/python-app.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)
```