class CloudAccessKey(models.Model): cloud_type_choices = (('aliyun', 'aliyun'),) type = models.CharField(max_length=20, default='', choices=cloud_type_choices) key_id = models.CharField(max_length=200) key_secret = models.CharField(max_length=200) remark = models.CharField(max_length=50, default='', blank=True) def __init__(self, *args, **kwargs): self.c = Crypto() super().__init__(*args, **kwargs) @property def raw_key_id(self): """ 返回明文信息""" return self.c.decrypt(self.key_id) @property def raw_key_secret(self): """ 返回明文信息""" return self.c.decrypt(self.key_secret) def save(self, *args, **kwargs): self.key_id = self.c.encrypt(self.key_id) self.key_secret = self.c.encrypt(self.key_secret) super(CloudAccessKey, self).save(*args, **kwargs) def __str__(self): return f'{self.type}({self.remark})' class Meta: managed = True db_table = 'cloud_access_key' verbose_name = u'云服务认证信息配置' verbose_name_plural = u'云服务认证信息配置'
class TestCryptoECB(TestCase): def setUp(self): self.crypto = Crypto(mode='ECB') self.value = 'hello,text' self.encrypted = "4DIIbNsZPqO1DuXX1GjpkQ==" def test_ecb_encrypt(self): self.assertEqual(self.crypto.encrypt(self.value), self.encrypted) def test_ecb_decrypt(self): self.assertEqual(self.crypto.decrypt(self.encrypted), self.value)
class TestCrypto(TestCase): def setUp(self): self.crypto = Crypto() self.value = 'hello,text' self.encrypted = 'pyy1FL2ftjBjUrJlGjgl3g==' def test_encrypt(self): self.assertEqual(self.crypto.encrypt(self.value), self.encrypted) def test_decrypt(self): self.assertEqual(self.crypto.decrypt(self.encrypted), self.value)
class TestCryptoCBC(TestCase): def setUp(self) -> None: self.crypto = Crypto(mode='CBC') self.value = 'hello,text' self.encrypted = "E_RFOSafjW-FQ-PDkXkv5g==" def test_cbc_encrypt(self): self.assertEqual(self.crypto.encrypt(self.value), self.encrypted) def test_cbc_decrypt(self): self.assertEqual(self.crypto.decrypt(self.encrypted), self.value)
class TestCrypto(TestCase): def setUp(self): self.crypto = Crypto() self.value = 'hello,text' if getattr(settings, "MIRAGE_SECRET_KEY", None): self.encrypted = "4DIIbNsZPqO1DuXX1GjpkQ==" else: self.encrypted = 'pyy1FL2ftjBjUrJlGjgl3g==' def test_encrypt(self): self.assertEqual(self.crypto.encrypt(self.value), self.encrypted) def test_decrypt(self): self.assertEqual(self.crypto.decrypt(self.encrypted), self.value)