def test_encrypt_message(self): origin_crypto = _crypto.PrpCrypto _crypto.PrpCrypto = PrpCryptoMock nonce = 'nEXhMP4r' timestamp = '1445827045067' msg = """{"EventType":"check_create_suite_url","Random":"LPIdSnlF","TestSuiteKey":"suite4xxxxxxxxxxxxxxx"}""" expected = { 'msg_signature': 'bcf6dcefa4ce2dbaf7b0666c7264d46fd9aad4bd', 'encrypt': '5DJFWzjRNOQk+5GSZxW+VrFMDWCIidPjEjg3//gm5x556BedVi62rDj1F9uXU97a4jw1R4FACUv9RCpoDobNqxhxRB2Yt' 'W901k4KHbP1/wpFJ3xdLG0n0A8U1VhENg80zKJd+YROR0YMGum4WYuoXJ6J98vt0ihYeIFoapNddLML5MyNAGM9saSpko' 'uDMSvD+iU14i7V8ix1ia1Tb9ogog==', 'timeStamp': '1445827045067', 'nonce': 'nEXhMP4r' } crypto = DingTalkCrypto(self.token, self.encoding_aes_key, self.suite_key) encrypted = crypto.encrypt_message(msg, nonce, timestamp) _crypto.PrpCrypto = origin_crypto self.assertEqual(expected.keys(), encrypted.keys()) for key in expected.keys(): self.assertEqual(expected[key], encrypted[key])
def __init__(self, corp_id, corp_secret, token=None, aes_key=None, storage=None, timeout=None, auto_retry=True): super(SecretClient, self).__init__(corp_id, 'secret:' + corp_id, storage, timeout, auto_retry) self.corp_secret = corp_secret self.crypto = DingTalkCrypto(token, aes_key, corp_id)
def __init__(self, suite_key, suite_secret, token=None, aes_key=None, storage=None, timeout=None, auto_retry=True): super(ISVClient, self).__init__(storage, timeout, auto_retry) self.suite_key = suite_key self.suite_secret = suite_secret self.cache = ISVCache(self.storage, 'isv:' + self.suite_key) self.crypto = DingTalkCrypto(token, aes_key, suite_key)
def test_decrypt_encrypt_str_should_fail(self): from dingtalk.core.exceptions import InvalidSignatureException signature = '5a65ceeef9aab2d149439f82dc191dd6c5cbe2c0' timestamp = '1445827045067' nonce = 'xxxxx' encrypt_str = b'1a3NBxmCFwkCJvfoQ7WhJHB+iX3qHPsc9JbaDznE1i03peOk1LaOQoRz3+nlyGNhwmwJ3vDMG+OzrHMeiZI7gTRWVdUB' \ b'mfxjZ8Ej23JVYa9VrYeJ5as7XM/ZpulX8NEQis44w53h1qAgnC3PRzM7Zc/D6Ibr0rgUathB6zRHP8PYrfgnNOS9PhSB' \ b'dHlegK+AGGanfwjXuQ9+0pZcy0w9lQ==' crypto = DingTalkCrypto(self.token, self.encoding_aes_key, self.suite_key) self.assertRaises(InvalidSignatureException, crypto.decrypt_encrypt_str, signature, timestamp, nonce, encrypt_str)
def test_decrypt_encrypt_str(self): from dingtalk.core.utils import to_text signature = '5a65ceeef9aab2d149439f82dc191dd6c5cbe2c0' timestamp = '1445827045067' nonce = 'nEXhMP4r' encrypt_str = '1a3NBxmCFwkCJvfoQ7WhJHB+iX3qHPsc9JbaDznE1i03peOk1LaOQoRz3+nlyGNhwmwJ3vDMG+OzrHMeiZI7gTRWVdUBm' \ 'fxjZ8Ej23JVYa9VrYeJ5as7XM/ZpulX8NEQis44w53h1qAgnC3PRzM7Zc/D6Ibr0rgUathB6zRHP8PYrfgnNOS9PhSBdH' \ 'legK+AGGanfwjXuQ9+0pZcy0w9lQ==' crypto = DingTalkCrypto(self.token, self.encoding_aes_key, self.suite_key) msg = crypto.decrypt_encrypt_str(signature, timestamp, nonce, encrypt_str) msg_dict = json.loads(to_text(msg)) self.assertEqual('check_create_suite_url', msg_dict['EventType']) self.assertEqual('LPIdSnlF', msg_dict['Random']) self.assertEqual(self.suite_key, msg_dict['TestSuiteKey'])