Search in sources :

Example 1 with KeysetToJsonResponse

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);
}
Also used : ByteString(com.google.protobuf.ByteString) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) KeysetToJsonResponse(com.google.crypto.tink.proto.testing.KeysetToJsonResponse) KeysetFromJsonResponse(com.google.crypto.tink.proto.testing.KeysetFromJsonResponse) Test(org.junit.Test)

Example 2 with KeysetToJsonResponse

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();
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) CleartextKeysetHandle(com.google.crypto.tink.CleartextKeysetHandle) Keyset(com.google.crypto.tink.proto.Keyset) GeneralSecurityException(java.security.GeneralSecurityException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) KeysetToJsonResponse(com.google.crypto.tink.proto.testing.KeysetToJsonResponse)

Aggregations

KeysetToJsonResponse (com.google.crypto.tink.proto.testing.KeysetToJsonResponse)2 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)1 KeysetHandle (com.google.crypto.tink.KeysetHandle)1 Keyset (com.google.crypto.tink.proto.Keyset)1 KeysetFromJsonResponse (com.google.crypto.tink.proto.testing.KeysetFromJsonResponse)1 KeysetGenerateResponse (com.google.crypto.tink.proto.testing.KeysetGenerateResponse)1 ByteString (com.google.protobuf.ByteString)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Test (org.junit.Test)1