def test_cbc_channel_cipher(self): key = six.b( '\x93\xe3\x5c\xc9\x77\x53\xfd\x1a' '\x79\xb4\xd8\x84\xe7\xdc\xfd\xdf' ) iv = six.b( '\x28\x4c\xe4\x8d\x4b\xdc\x9d\x42' '\x8a\x77\x6b\x53\x2d\xc7\xb5\xc0' ) log.debug("KEY_LEN: %d" % len(key)) log.debug("IV_LEN: %d" % len(iv)) cipher = get_cipher({'key': key, 'iv': iv}) plaintext = six.b("The quick brown fox") expected_ciphertext = six.b( '\x28\x4c\xe4\x8d\x4b\xdc\x9d\x42' '\x8a\x77\x6b\x53\x2d\xc7\xb5\xc0' '\x83\x5c\xcf\xce\x0c\xfd\xbe\x37' '\xb7\x92\x12\x04\x1d\x45\x68\xa4' '\xdf\x7f\x6e\x38\x17\x4a\xff\x50' '\x73\x23\xbb\xca\x16\xb0\xe2\x84' ) actual_ciphertext = cipher.encrypt(plaintext) self.assertEqual(expected_ciphertext, actual_ciphertext)
def test_cbc_channel_cipher(self): key = six.b( '\x93\xe3\x5c\xc9\x77\x53\xfd\x1a' '\x79\xb4\xd8\x84\xe7\xdc\xfd\xdf' ) iv = six.b( '\x28\x4c\xe4\x8d\x4b\xdc\x9d\x42' '\x8a\x77\x6b\x53\x2d\xc7\xb5\xc0' ) log.debug("KEY_LEN: %d" % len(key)) log.debug("IV_LEN: %d" % len(iv)) cipher = get_cipher({'key': key, 'iv': iv}) plaintext = six.b("The quick brown fox") expected_ciphertext = six.b( '\x28\x4c\xe4\x8d\x4b\xdc\x9d\x42' '\x8a\x77\x6b\x53\x2d\xc7\xb5\xc0' '\x83\x5c\xcf\xce\x0c\xfd\xbe\x37' '\xb7\x92\x12\x04\x1d\x45\x68\xa4' '\xdf\x7f\x6e\x38\x17\x4a\xff\x50' '\x73\x23\xbb\xca\x16\xb0\xe2\x84' ) actual_ciphertext = cipher.encrypt(plaintext) assert expected_ciphertext == actual_ciphertext
def options(self, options): self.__options = options if options and options.encrypted: self.__cipher = get_cipher(options.cipher_params) else: self.__cipher = None
def options(self, options): self.__options = options if options and 'cipher' in options: cipher = options.get('cipher') if cipher is not None: cipher = get_cipher(cipher) self.__cipher = cipher
def options(self, options): self.__options = options if options and 'cipher' in options: if options.get('cipher') is not None: self.__cipher = get_cipher(options.get('cipher')) else: self.__cipher = None
def __init__(self, ably, name, options): self.__ably = ably self.__name = name self.__options = options self.__base_path = '/channels/%s/' % quote(name) self.__presence = Presence(self) if options and options.encrypted: self.__cipher = get_cipher(options.cipher_params) else: self.__cipher = None
def setUpClass(cls): with open(os.path.dirname(__file__) + '/../../submodules/test-resources/%s' % cls.fixture_file, 'r') as f: cls.fixture = json.loads(f.read()) cls.params = { 'secret_key': base64.b64decode(cls.fixture['key'].encode('ascii')), 'mode': cls.fixture['mode'], 'algorithm': cls.fixture['algorithm'], 'iv': base64.b64decode(cls.fixture['iv'].encode('ascii')), } cls.cipher_params = CipherParams(**cls.params) cls.cipher = get_cipher(cls.cipher_params) cls.items = cls.fixture['items']
def setUpClass(cls): with open(os.path.dirname(__file__) + "/../../submodules/test-resources/%s" % cls.fixture_file, "r") as f: cls.fixture = json.loads(f.read()) cls.params = { "secret_key": base64.b64decode(cls.fixture["key"].encode("ascii")), "mode": cls.fixture["mode"], "algorithm": cls.fixture["algorithm"], "iv": base64.b64decode(cls.fixture["iv"].encode("ascii")), } cls.cipher_params = CipherParams(**cls.params) cls.cipher = get_cipher(cls.cipher_params) cls.items = cls.fixture["items"]
def test_encode(self): for item in self.items: # need to reset iv self.cipher_params = CipherParams(**self.params) self.cipher = get_cipher(self.cipher_params) data = self.get_encoded(item['encoded']) expected = item['encrypted'] message = Message(item['encoded']['name'], data) message.encrypt(self.cipher) as_dict = message.as_dict() self.assertEqual(as_dict['data'], expected['data']) self.assertEqual(as_dict['encoding'], expected['encoding'])
def test_encode(self): for item in self.items: # need to reset iv self.cipher_params = CipherParams(**self.params) self.cipher = get_cipher(self.cipher_params) data = self.get_encoded(item['encoded']) expected = item['encrypted'] message = Message(item['encoded']['name'], data) message.encrypt(self.cipher) as_dict = message.as_dict() assert as_dict['data'] == expected['data'] assert as_dict['encoding'] == expected['encoding']
def test_encode(self): for item in self.items: # need to reset iv self.cipher_params = CipherParams(**self.params) self.cipher = get_cipher(self.cipher_params) data = self.get_encoded(item["encoded"]) expected = item["encrypted"] message = Message(item["encoded"]["name"], data) message.encrypt(self.cipher) as_dict = message.as_dict() self.assertEqual(as_dict["data"], expected["data"]) self.assertEqual(as_dict["encoding"], expected["encoding"])
def test_crypto_encrypted_unhandled(self): channel_name = self.get_channel_name('persisted:crypto_send_encrypted_unhandled') key = six.b('0123456789abcdef') data = six.u('foobar') publish0 = self.ably.channels.get(channel_name, cipher={'key': key}) publish0.publish("publish0", data) rx_channel = self.ably2.channels[channel_name] history = rx_channel.history() message = history.items[0] cipher = get_cipher(get_default_params({'key': key})) assert cipher.decrypt(message.data).decode() == data assert message.encoding == 'utf-8/cipher+aes-128-cbc'
async def test_crypto_encrypted_unhandled(self): channel_name = self.get_channel_name('persisted:crypto_send_encrypted_unhandled') key = b'0123456789abcdef' data = 'foobar' publish0 = self.ably.channels.get(channel_name, cipher={'key': key}) await publish0.publish("publish0", data) rx_channel = self.ably2.channels[channel_name] history = await rx_channel.history() message = history.items[0] cipher = get_cipher(get_default_params({'key': key})) assert cipher.decrypt(message.data).decode() == data assert message.encoding == 'utf-8/cipher+aes-128-cbc'
def test_crypto_encrypted_unhandled(self): channel_name = self.protocol_channel_name('persisted:crypto_send_encrypted_unhandled') key = six.b('0123456789abcdef') data = six.u('foobar') publish0 = self.ably.channels.get(channel_name, cipher={'key': key}) publish0.publish("publish0", data) rx_channel = self.ably2.channels[channel_name] history = rx_channel.history() message = history.items[0] cipher = get_cipher(get_default_params({'key': key})) self.assertEqual(cipher.decrypt(message.data).decode(), data) self.assertEqual(message.encoding, 'utf-8/cipher+aes-128-cbc')
def test_crypto_encrypted_unhandled(self): channel_name = self.protocol_channel_name("persisted:crypto_send_encrypted_unhandled") key = "0123456789abcdef" data = six.u("foobar") channel_options = ChannelOptions(encrypted=True, cipher_params=get_default_params(key)) publish0 = self.ably.channels.get(channel_name, channel_options) publish0.publish("publish0", data) rx_channel = self.ably2.channels[channel_name] history = rx_channel.history() message = history.items[0] cipher = get_cipher(get_default_params(key)) self.assertEqual(cipher.decrypt(message.data).decode(), data) self.assertEqual(message.encoding, "utf-8/cipher+aes-128-cbc")
def test_cbc_channel_cipher(self): key = six.b("\x93\xe3\x5c\xc9\x77\x53\xfd\x1a" "\x79\xb4\xd8\x84\xe7\xdc\xfd\xdf") iv = six.b("\x28\x4c\xe4\x8d\x4b\xdc\x9d\x42" "\x8a\x77\x6b\x53\x2d\xc7\xb5\xc0") log.debug("KEY_LEN: %d" % len(key)) log.debug("IV_LEN: %d" % len(iv)) cipher = get_cipher(CipherParams(secret_key=key, iv=iv)) plaintext = six.b("The quick brown fox") expected_ciphertext = six.b( "\x28\x4c\xe4\x8d\x4b\xdc\x9d\x42" "\x8a\x77\x6b\x53\x2d\xc7\xb5\xc0" "\x83\x5c\xcf\xce\x0c\xfd\xbe\x37" "\xb7\x92\x12\x04\x1d\x45\x68\xa4" "\xdf\x7f\x6e\x38\x17\x4a\xff\x50" "\x73\x23\xbb\xca\x16\xb0\xe2\x84" ) actual_ciphertext = cipher.encrypt(plaintext) self.assertEqual(expected_ciphertext, actual_ciphertext)
def decrypt(self, payload, options={}): cipher = get_cipher({'key': b'keyfordecrypt_16'}) return cipher.decrypt(payload)
def decrypt(self, payload, options={}): ciphertext = base64.b64decode(payload.encode('ascii')) cipher = get_cipher({'key': b'keyfordecrypt_16'}) return cipher.decrypt(ciphertext)
def decrypt(self, payload, options=None): if options is None: options = {} cipher = get_cipher({'key': b'keyfordecrypt_16'}) return cipher.decrypt(payload)
def decrypt(self, payload, options={}): ciphertext = base64.b64decode(payload.encode('ascii')) cipher = get_cipher(get_default_params('keyfordecrypt_16')) return cipher.decrypt(ciphertext)
def decrypt(self, payload, options={}): cipher = get_cipher(get_default_params('keyfordecrypt_16')) return cipher.decrypt(payload)