使用 Flask-SQLAlchemy 存储服务帐户

Storing a service account with Flask-SQLAlchemy

我正在使用带有本地数据库的 Flask 和 Flask-SQLAlchemy 编写一个小型 HipChat 插件。我希望管理员能够为要与之集成的外部服务设置服务帐户。

因为需要存储服务帐户的 username/password 以便集成可以使用它们进行 API 调用 我不能使用不可逆的哈希方法来存储密码.

是否有关于如何处理此问题以便更好地保护密码或数据库的建议?

您可以在将数据存储到数据库之前对其进行加密。 pycrypto 是您可以使用的库之一。

It is easy to encrypt text using DES/ECB with pycrypto. The key ‘10234567’ is 8 bytes and the text’s length needs to be a multiple of 8 bytes. We picked ‘abcdefgh’ in this example.

>>> from Crypto.Cipher import DES
>>> des = DES.new('01234567', DES.MODE_ECB)
>>> text = 'abcdefgh'
>>> cipher_text = des.encrypt(text)
>>> cipher_text
'\xec\xc2\x9e\xd9] a\xd0'
>>> des.decrypt(cipher_text)
'abcdefgh'

here 是一篇关于 pycrypto 的短文。