use of com.google.crypto.tink.proto.testing.KeysetToJsonResponse in project tink by google.
the class TestingServicesTest method toFromJson_success.
@Test
public void toFromJson_success() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("AES128_GCM"));
KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
assertThat(keysetResponse.getErr()).isEmpty();
byte[] keyset = keysetResponse.getKeyset().toByteArray();
KeysetToJsonResponse toResponse = keysetToJson(keysetStub, keyset);
assertThat(toResponse.getErr()).isEmpty();
String jsonKeyset = toResponse.getJsonKeyset();
KeysetFromJsonResponse fromResponse = keysetFromJson(keysetStub, jsonKeyset);
assertThat(fromResponse.getErr()).isEmpty();
byte[] output = fromResponse.getKeyset().toByteArray();
assertThat(output).isEqualTo(keyset);
}
use of com.google.crypto.tink.proto.testing.KeysetToJsonResponse in project tink by google.
the class KeysetServiceImpl method toJson.
@Override
public void toJson(KeysetToJsonRequest request, StreamObserver<KeysetToJsonResponse> responseObserver) {
KeysetToJsonResponse response;
try {
KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
Keyset keyset = CleartextKeysetHandle.getKeyset(keysetHandle);
ByteArrayOutputStream jsonKeysetStream = new ByteArrayOutputStream();
JsonKeysetWriter.withOutputStream(jsonKeysetStream).write(keyset);
jsonKeysetStream.close();
response = KeysetToJsonResponse.newBuilder().setJsonKeyset(jsonKeysetStream.toString("UTF-8")).build();
} catch (GeneralSecurityException | InvalidProtocolBufferException e) {
response = KeysetToJsonResponse.newBuilder().setErr(e.toString()).build();
} catch (IOException e) {
responseObserver.onError(Status.UNKNOWN.withDescription(e.getMessage()).asException());
return;
}
responseObserver.onNext(response);
responseObserver.onCompleted();
}
Aggregations