def setUp(self): self.crypto = Crypto() self.value = 'hello,text' if getattr(settings, "MIRAGE_SECRET_KEY", None): self.encrypted = "4DIIbNsZPqO1DuXX1GjpkQ==" else: self.encrypted = 'pyy1FL2ftjBjUrJlGjgl3g=='
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 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 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 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)
def __init__(self, *args, **kwargs): self.c = Crypto() super().__init__(*args, **kwargs)
""" Dict内容加解密 """ import re from mirage.crypto import Crypto crypto = Crypto() # 需加密的敏感词 sensitive_keys = ( 'key', 'token', 'secret', 'password', 'signature', # 通用 'vin', 'owner', 'address', 'name', 'number', 'authority', # OCR识别 'caller', 'called', 'dsc', 'src', # 云讯双呼 ) # 需加密的敏感词正则编译 sensitive_pattern = re.compile('|'.join(sensitive_keys), re.I) def encrypt_dic(dic: dict) -> dict: """ Dict敏感字段加密,仅支持字符串 """ for key, val in dic.items(): if not sensitive_pattern.search(key): if isinstance(val, dict): dic[key] = encrypt_dic(val) else: dic[key] = val continue if isinstance(val, str): dic[key] = crypto.encrypt(val)
def setUp(self): self.crypto = Crypto() self.value = 'hello,text' self.encrypted = 'pyy1FL2ftjBjUrJlGjgl3g=='
def setUp(self): self.crypto = Crypto(mode='ECB') self.value = 'hello,text' self.encrypted = "4DIIbNsZPqO1DuXX1GjpkQ=="
def setUp(self) -> None: self.crypto = Crypto(mode='CBC') self.value = 'hello,text' self.encrypted = "E_RFOSafjW-FQ-PDkXkv5g=="