我必须使用 Django 在我的数据库中存储一些敏感信息。
我有一个 Client 模型,每个客户端都有一堆 SocialAccounts(twitter、fb 等),带有 URL、客户端和密码。
考虑到属于“管理员”组的所有用户都应该能够看到密码。将这些密码存储在数据库中的安全方法是什么?
请您参考如下方法:
显然 django-extensions 有 two fields for this very purpose :
- EncryptedCharField - CharField which transparently encrypts its value as it goes in and out of the database. Encryption is handled by Keyczar. To use this field you must have Keyczar installed, have generated a primary encryption key, and have settings.KEYS_DIR set to the full path of your keys directory.
- EncryptedTextField - CharField which transparently encrypts its value as it goes in and out of the database. Encryption is handled by Keyczar. To use this field you must have Keyczar installed, have generated a primary encryption key, and have settings.KEYS_DIR set to the full path of your keys directory.
所以基本上我必须为 python 安装 (1) keyczar 及其依赖项:
pip install https://keyczar.googlecode.com/files/python-keyczar-0.71c.tar.gz
pip install pycrypto
pip install pyasn1
(如果你还没有,...也安装 django-extensions。)
(2) 创建一个目录,您将在其中存储您的 key 并创建您的 key :
mkdir keys
python path/to/keyczart.py create --location='keys' --purpose='crypt' --name='whatever_name'
python path/to/keyczart.py addkey --location='keys' --status='primary'
(3) 将目录添加到您的 settings.py 下的 ENCRYPTED_FIELD_KEYS_DIR。
最后 (4) 将 EncryptedCharField 或 EncrytedTextField 添加到您的模型中:
from django_extensions.db.fields.encrypted import EncryptedCharField
class SocialAccount(models.Model):
platform = models.ForeignKey(SocialPlatformType,
related_name='platforms')
url = models.URLField('Account url', unique=True,
null=True, blank=True)
password = EncryptedCharField(null=True, blank=True, max_length=255)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
def __unicode__(self):
return self.url
我希望有人觉得这很有用。
