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();
}
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);
}
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);
}
Aggregations