Skip to main content

Encrypt and decrypt fields for a Django, SQLAlchemy ORM models or for direct use Crypto class

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.

ProjectCheck

How install

pip install encrypt-decrypt-fields

Usage

For Django use project secret key or own:

from django.db import models
from encrypt_decrypt_fields import EncryptedBinaryField


class DemoModel(models.Model):
    password = EncryptedBinaryField(blank=True, null=True)
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:

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:

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)  

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

encrypt_decrypt_fields-1.3.6.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

encrypt_decrypt_fields-1.3.6-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file encrypt_decrypt_fields-1.3.6.tar.gz.

File metadata

  • Download URL: encrypt_decrypt_fields-1.3.6.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.10.10 Darwin/23.0.0

File hashes

Hashes for encrypt_decrypt_fields-1.3.6.tar.gz
Algorithm Hash digest
SHA256 ce2834d0da1a61868cececca20796ee261f379f8aaee786186ff0fe1cd8220f1
MD5 18739473fb6623189eb470ba835a353f
BLAKE2b-256 68571e99f55679bd6f603a22c7efa8e104ba92d052f349ad0b30a8b354a7c0b9

See more details on using hashes here.

File details

Details for the file encrypt_decrypt_fields-1.3.6-py3-none-any.whl.

File metadata

File hashes

Hashes for encrypt_decrypt_fields-1.3.6-py3-none-any.whl
Algorithm Hash digest
SHA256 2ef69f9e6182c44e12a5b87abb3e8683566ff97bbc2aa8c398bef9393b20b87f
MD5 9c92d3e61dd0251e396a484e25aed528
BLAKE2b-256 57d947fcf907c2d196b1feea2430f8b4f3ac12c6cff8c3873ff754b5cfad4bdc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page