Search in sources :

Example 1 with KeysetFromJsonResponse

use of com.google.crypto.tink.proto.testing.KeysetFromJsonResponse in project tink by google.

the class KeysetServiceImpl method fromJson.

@Override
public void fromJson(KeysetFromJsonRequest request, StreamObserver<KeysetFromJsonResponse> responseObserver) {
    KeysetFromJsonResponse response;
    try {
        KeysetHandle keysetHandle = CleartextKeysetHandle.read(JsonKeysetReader.withString(request.getJsonKeyset()));
        Keyset keyset = CleartextKeysetHandle.getKeyset(keysetHandle);
        ByteArrayOutputStream keysetStream = new ByteArrayOutputStream();
        BinaryKeysetWriter.withOutputStream(keysetStream).write(keyset);
        keysetStream.close();
        response = KeysetFromJsonResponse.newBuilder().setKeyset(ByteString.copyFrom(keysetStream.toByteArray())).build();
    } catch (GeneralSecurityException | InvalidProtocolBufferException e) {
        response = KeysetFromJsonResponse.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) KeysetFromJsonResponse(com.google.crypto.tink.proto.testing.KeysetFromJsonResponse)

Example 2 with KeysetFromJsonResponse

use of com.google.crypto.tink.proto.testing.KeysetFromJsonResponse in project tink by google.

the class TestingServicesTest method toJson_success.

@Test
public void toJson_success() throws Exception {
    String jsonKeyset = "" + "{" + "  \"primaryKeyId\": 42," + "  \"key\": [" + "    {" + "      \"keyData\": {" + "        \"typeUrl\": \"type.googleapis.com/google.crypto.tink.AesGcmKey\"," + "        \"keyMaterialType\": \"SYMMETRIC\"," + "        \"value\": \"AFakeTestKeyValue1234567\"" + "      }," + "      \"outputPrefixType\": \"TINK\"," + "      \"keyId\": 42," + "      \"status\": \"ENABLED\"" + "    }" + "  ]" + "}";
    KeysetFromJsonResponse fromResponse = keysetFromJson(keysetStub, jsonKeyset);
    assertThat(fromResponse.getErr()).isEmpty();
    byte[] output = fromResponse.getKeyset().toByteArray();
    Keyset keyset = BinaryKeysetReader.withBytes(output).read();
    assertThat(keyset.getPrimaryKeyId()).isEqualTo(42);
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) ByteString(com.google.protobuf.ByteString) KeysetFromJsonResponse(com.google.crypto.tink.proto.testing.KeysetFromJsonResponse) Test(org.junit.Test)

Example 3 with KeysetFromJsonResponse

use of com.google.crypto.tink.proto.testing.KeysetFromJsonResponse 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)

Aggregations

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