Encrypt and decrypt for django model field.
Project description
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.
How install
pip install encrypt-decrypt-fields
Usage
For Django use project secret key or own:
from django.conf import settings
from django.db import Model
from django_encrypt_decrypt import EncryptedBinaryField
class DemoModel(models.Models):
password = EncryptedBinaryField(blank=True, null=True)
DemoModel.objects.create(password='password')
obj = DemoModel.objects.get(id=1)
obj.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:
from django.conf import settings
from django_encrypt_decrypt import Crypto
obj = DemoModel.objects.get(id=1)
decrypted = Crypto(settings.SECRET_KEY).decrypt_token(obj.password.to_bytes())
decrypted # 'password'
For SQLAlchemy, it is similar:
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base
from sqlalchemy import Column, Integer, String
Base = declarative_base()
class Demo(Base):
__tablename__ = 'demo'
id = Column(Integer, primary_key=True)
name = Column(String)
password = Column(EncryptedAlchemyBinaryField(key='secret), nullable=True)
object = session.query(Demo).first()
Crypto('secret').decrypt_token(object.password)
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
Built Distribution
Close
Hashes for encrypt-decrypt-fields-1.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e27afb9a4c9c6ebe3e531a25497c0b100fb8cd5cdd0cc573b17b2f82428fa9b |
|
MD5 | 9d1999fa9fde9947ec275a96ae00e79f |
|
BLAKE2b-256 | dd8ec7f0e402937fbc80b6c30b1ddcd5e8bdf80ab0f7676320dbdb9ab41ccd15 |
Close
Hashes for encrypt_decrypt_fields-1.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7e6d0f902706a91fdb20cbaed895a7d2996ab3af54da78f8121796ca8f54f81 |
|
MD5 | ece5d842b8d8cc1496899a0795b0b7fc |
|
BLAKE2b-256 | 8fe750f3710e532ca28cbda2ddcc0d94507741151550295502978735590b580c |