def test_generate_streaming_encrypt_decrypt(self): keyset_servicer = services.KeysetServicer() streaming_aead_servicer = services.StreamingAeadServicer() templates = streaming_aead.streaming_aead_key_templates template = templates.AES128_CTR_HMAC_SHA256_4KB.SerializeToString() gen_request = testing_api_pb2.KeysetGenerateRequest(template=template) gen_response = keyset_servicer.Generate(gen_request, self._ctx) self.assertEqual(gen_response.WhichOneof('result'), 'keyset') keyset = gen_response.keyset plaintext = b'The quick brown fox jumps over the lazy dog' associated_data = b'associated_data' enc_request = testing_api_pb2.StreamingAeadEncryptRequest( keyset=keyset, plaintext=plaintext, associated_data=associated_data) enc_response = streaming_aead_servicer.Encrypt(enc_request, self._ctx) self.assertEqual(enc_response.WhichOneof('result'), 'ciphertext') ciphertext = enc_response.ciphertext dec_request = testing_api_pb2.StreamingAeadDecryptRequest( keyset=keyset, ciphertext=ciphertext, associated_data=associated_data) dec_response = streaming_aead_servicer.Decrypt(dec_request, self._ctx) self.assertEqual(dec_response.WhichOneof('result'), 'plaintext') self.assertEqual(dec_response.plaintext, plaintext)
def new_encrypting_stream(self, plaintext: BinaryIO, associated_data: bytes) -> BinaryIO: enc_request = testing_api_pb2.StreamingAeadEncryptRequest( keyset=self._keyset, plaintext=plaintext.read(), associated_data=associated_data) enc_response = self._stub.Encrypt(enc_request) if enc_response.err: raise tink.TinkError(enc_response.err) return io.BytesIO(enc_response.ciphertext)
def new_encrypting_stream(self, plaintext: BinaryIO, associated_data: bytes) -> BinaryIO: logging.info('encrypt in lang %s.', self.lang) logging.info('type(plaintext): %s', type(plaintext)) enc_request = testing_api_pb2.StreamingAeadEncryptRequest( keyset=self._keyset, plaintext=plaintext.read(), associated_data=associated_data) enc_response = self._stub.Encrypt(enc_request) if enc_response.err: logging.info('error encrypt in %s: %s', self.lang, enc_response.err) raise tink.TinkError(enc_response.err) return io.BytesIO(enc_response.ciphertext)