在运行时为 SQLAlchemy 的“EncryptedType”指定密钥
Specifying a key for SQLAlchemy's `EncryptedType` at runtime
SQLAlchemy-Utils documentation for the EncryptedType
column type 有一个看起来像这样的例子:
secret_key = 'secretkey1234'
# setup
engine = create_engine('sqlite:///:memory:')
connection = engine.connect()
Base = declarative_base()
class User(Base):
__tablename__ = "user"
id = sa.Column(sa.Integer, primary_key=True)
username = sa.Column(EncryptedType(sa.Unicode,
secret_key,
AesEngine,
'pkcs5'))
但是如果我在定义 User
class 之前不知道密钥是什么怎么办?比如我想提示用户输入秘钥怎么办?
这是您链接到的 docs 中的最后一个示例:
The key parameter accepts a callable to allow for the key to change
per-row instead of being fixed for the whole table.
def get_key():
return 'dynamic-key'
class User(Base):
__tablename__ = 'user'
id = sa.Column(sa.Integer, primary_key=True)
username = sa.Column(EncryptedType(
sa.Unicode, get_key))
SQLAlchemy-Utils documentation for the EncryptedType
column type 有一个看起来像这样的例子:
secret_key = 'secretkey1234'
# setup
engine = create_engine('sqlite:///:memory:')
connection = engine.connect()
Base = declarative_base()
class User(Base):
__tablename__ = "user"
id = sa.Column(sa.Integer, primary_key=True)
username = sa.Column(EncryptedType(sa.Unicode,
secret_key,
AesEngine,
'pkcs5'))
但是如果我在定义 User
class 之前不知道密钥是什么怎么办?比如我想提示用户输入秘钥怎么办?
这是您链接到的 docs 中的最后一个示例:
The key parameter accepts a callable to allow for the key to change per-row instead of being fixed for the whole table.
def get_key():
return 'dynamic-key'
class User(Base):
__tablename__ = 'user'
id = sa.Column(sa.Integer, primary_key=True)
username = sa.Column(EncryptedType(
sa.Unicode, get_key))