python encrypted_column.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python encrypted_column.py相关的知识,希望对你有一定的参考价值。

from cryptography.fernet import Fernet
from sqlalchemy.ext.mutable import MutableDict
from sqlalchemy.types import TypeDecorator, BINARY

secret_key = 'change me!'

class EncryptedTypeBase(TypeDecorator):
    impl = BINARY

    def process_bind_param(self, value, dialect):
        if value is not None:
            value = EncryptedType.cipher_suite().encrypt(json.dumps(value).encode('utf-8'))
        return value

    def process_result_value(self, value, dialect):
        if value is not None:
            value = json.loads(EncryptedType.cipher_suite().decrypt(value).decode('utf-8'))
        return value

    @classmethod
    def cipher_suite(cls):
        if getattr(cls, '_cipher_suite', None) is None:
            cls._cipher_suite = Fernet(secret_key)
        return cls._cipher_suite

    @property
    def python_type(self):
        raise NotImplementedError()

    def process_literal_param(self, value, dialect):
        raise NotImplementedError()


EncryptedType = MutableDict.as_mutable(EncryptedTypeBase)

以上是关于python encrypted_column.py的主要内容,如果未能解决你的问题,请参考以下文章

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python

python初识