def _deserialize_entity(self, ctxt, entity): try: if self._key is not None: estr = cu.decrypt_data(cu.decode_data(entity), self._key) entity = jsonutils.loads(estr) except (ValueError, TypeError): return entity return entity
def _deserialize_context(self, ctxt): try: if self._key is not None: cstr = cu.decrypt_data(cu.decode_data(ctxt['context']), self._key) ctxt = jsonutils.loads(cstr) except (ValueError, TypeError): return ctxt return ctxt
def test_encryp_decrypt(self): key = 'my_secure_key' for size in range(1, 100): orig_data = Random.new().read(size) orig_encoded = crypto_utils.encode_data(orig_data) encrypted = crypto_utils.encrypt_data(orig_encoded, key) encoded = crypto_utils.encode_data(encrypted) decoded = crypto_utils.decode_data(encoded) decrypted = crypto_utils.decrypt_data(decoded, key) final_decoded = crypto_utils.decode_data(decrypted) self.assertEqual(orig_data, final_decoded, "Decrypted data did not match original")
def test_encryp_decrypt(self): key = 'my_secure_key' for size in range(1, 100): orig_data = os.urandom(size) orig_encoded = crypto_utils.encode_data(orig_data) encrypted = crypto_utils.encrypt_data(orig_encoded, key) encoded = crypto_utils.encode_data(encrypted) decoded = crypto_utils.decode_data(encoded) decrypted = crypto_utils.decrypt_data(decoded, key) final_decoded = crypto_utils.decode_data(decrypted) self.assertEqual(orig_data, final_decoded, "Decrypted data did not match original")
def _deserialize_entity(self, ctxt, entity): try: entity = jsonutils.loads(entity) instance_id = entity['csz-instance-id'] except (ValueError, TypeError): return entity instance_key = get_instance_encryption_key(instance_id) estr = cu.decrypt_data(cu.decode_data(entity['entity']), instance_key) entity = jsonutils.loads(estr) return entity
def _deserialize_context(self, ctxt): try: instance_id = ctxt.get('csz-instance-id', None) if instance_id is not None: instance_key = get_instance_encryption_key(instance_id) cstr = cu.decrypt_data(cu.decode_data(ctxt['context']), instance_key) ctxt = jsonutils.loads(cstr) except (ValueError, TypeError): return ctxt ctxt['instance_id'] = instance_id return ctxt
def test_decrypt(self): key = 'my_secure_key' for encoded, expected in ( # byte string: b'Hello World!' ('ZUhoNGVIaDRlSGg0ZUhoNL9PmM70hVcQ7j/kYF7Pw+BT7VSfsht0VsCIxy' 'KNN0NH', b'Hello World!'), # Unicoded string: u'Unicode:\u20ac' ('ZUhoNGVIaDRlSGg0ZUhoNIHZLIuIcQCRwWY7PR2y7JcqoDf4ViqXIfh0uE' 'Rbg9BA', b'Unicode:\xe2\x82\xac'), ): decoded = crypto_utils.decode_data(encoded) decrypted = crypto_utils.decrypt_data(decoded, key) final_decoded = crypto_utils.decode_data(decrypted) self.assertEqual(expected, final_decoded)
def deprocess_contents(processed_contents): encrypted_contents = crypto_utils.decode_data(processed_contents) return crypto_utils.decrypt_data( encrypted_contents, Modules.ENCRYPT_KEY)