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.

Check

How install

pip install encrypt-decrypt-fields

Usage

For Django use a 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) 
# b'gAAAAABgxGVVeTPV9i1nPNl91Ss4XVH0rD6eJCgOWIOeRwtagp12gBJg9DL_HXODTDW0WKsqc8Z9vsuHUiAr3qQVE9YQmTd3pg=='

To decrypt a 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)

service = Crypto(settings.SECRET_KEY)
decrypted = service.decrypt_token(obj.password)
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.7.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: encrypt_decrypt_fields-1.3.7.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.11.9 Darwin/24.6.0

File hashes

Hashes for encrypt_decrypt_fields-1.3.7.tar.gz
Algorithm Hash digest
SHA256 234297ad27561c1c007ca413edc113f1218c03c7755e72ec608fd24f3a0d6f07
MD5 5595affbf12fac5297aa067b8e8c637e
BLAKE2b-256 304c03d4bd0a495e35b40c8c85015b119d1bafdf92b56460b9b43837a4821deb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for encrypt_decrypt_fields-1.3.7-py3-none-any.whl
Algorithm Hash digest
SHA256 b3fef2baac365b218718a8c879ecd73afff4d5d86cd7b5e1dcbdb9044683debc
MD5 5f60683fd0ebdda402c8bee4808e8644
BLAKE2b-256 ab4411b8f84e1249cdcfd3df087a8c3a391e8cc4893b8bbda7a1f06e3eb98167

See more details on using hashes here.

Supported by

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