Example #1
0
    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")
Example #2
0
    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")
Example #3
0
    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)
Example #4
0
    def test_encode_decode_string(self):
        random_data = bytearray(Random.new().read(12))
        data = ['abc', 'numbers01234', '\x00\xFF\x00\xFF\xFF\x00', random_data]

        for datum in data:
            encoded_data = crypto_utils.encode_data(datum)
            decoded_data = crypto_utils.decode_data(encoded_data)
            self.assertEqual(datum, decoded_data, "Encode/decode failed")
Example #5
0
    def test_encode_decode_string(self):
        random_data = bytearray(Random.new().read(12))
        data = ['abc', 'numbers01234', '\x00\xFF\x00\xFF\xFF\x00', random_data]

        for datum in data:
            encoded_data = crypto_utils.encode_data(datum)
            decoded_data = crypto_utils.decode_data(encoded_data)
            self. assertEqual(datum, decoded_data,
                              "Encode/decode failed")
Example #6
0
    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)
Example #7
0
    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
Example #8
0
    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
Example #9
0
    def test_encode_decode_string(self):
        random_data = bytearray(Random.new().read(12))
        data = [b'abc', b'numbers01234', b'\x00\xFF\x00\xFF\xFF\x00',
                random_data, u'Unicode:\u20ac']

        for datum in data:
            encoded_data = crypto_utils.encode_data(datum)
            decoded_data = crypto_utils.decode_data(encoded_data)
            if isinstance(datum, six.text_type):
                decoded_data = decoded_data.decode('utf-8')
            self. assertEqual(datum, decoded_data,
                              "Encode/decode failed")
Example #10
0
    def test_encode_decode_string(self):
        random_data = bytearray(os.urandom(12))
        data = [b'abc', b'numbers01234', b'\x00\xFF\x00\xFF\xFF\x00',
                random_data, 'Unicode:\u20ac']

        for datum in data:
            encoded_data = crypto_utils.encode_data(datum)
            decoded_data = crypto_utils.decode_data(encoded_data)
            if isinstance(datum, str):
                decoded_data = decoded_data.decode('utf-8')
            self. assertEqual(datum, decoded_data,
                              "Encode/decode failed")
Example #11
0
    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_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
Example #14
0
    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
Example #15
0
 def deprocess_contents(processed_contents):
     encrypted_contents = crypto_utils.decode_data(processed_contents)
     return crypto_utils.decrypt_data(
         encrypted_contents, Modules.ENCRYPT_KEY)
Example #16
0
 def deprocess_contents(processed_contents):
     encrypted_contents = crypto_utils.decode_data(processed_contents)
     return crypto_utils.decrypt_data(
         encrypted_contents, Modules.ENCRYPT_KEY)