use of com.google.crypto.tink.proto.testing.ComputeMacResponse in project tink by google.
the class TestingServicesTest method verifyMac_failsOnBadKeyset.
@Test
public void verifyMac_failsOnBadKeyset() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(HmacKeyManager.hmacSha256HalfDigestTemplate());
byte[] data = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
assertThat(keysetResponse.getErr()).isEmpty();
byte[] keyset = keysetResponse.getKeyset().toByteArray();
ComputeMacResponse compResponse = computeMac(macStub, keyset, data);
assertThat(compResponse.getErr()).isEmpty();
byte[] macValue = compResponse.getMacValue().toByteArray();
byte[] badKeyset = "bad keyset".getBytes(UTF_8);
VerifyMacResponse verifyResponse = verifyMac(macStub, badKeyset, macValue, data);
assertThat(verifyResponse.getErr()).isNotEmpty();
}
use of com.google.crypto.tink.proto.testing.ComputeMacResponse in project tink by google.
the class TestingServicesTest method computeVerifyMac_success.
@Test
public void computeVerifyMac_success() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(HmacKeyManager.hmacSha256HalfDigestTemplate());
byte[] data = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
assertThat(keysetResponse.getErr()).isEmpty();
byte[] keyset = keysetResponse.getKeyset().toByteArray();
ComputeMacResponse compResponse = computeMac(macStub, keyset, data);
assertThat(compResponse.getErr()).isEmpty();
byte[] macValue = compResponse.getMacValue().toByteArray();
VerifyMacResponse verifyResponse = verifyMac(macStub, keyset, macValue, data);
assertThat(verifyResponse.getErr()).isEmpty();
}
use of com.google.crypto.tink.proto.testing.ComputeMacResponse in project tink by google.
the class MacServiceImpl method computeMac.
/**
* Encrypts a message.
*/
@Override
public void computeMac(ComputeMacRequest request, StreamObserver<ComputeMacResponse> responseObserver) {
ComputeMacResponse response;
try {
KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
Mac mac = keysetHandle.getPrimitive(Mac.class);
byte[] macValue = mac.computeMac(request.getData().toByteArray());
response = ComputeMacResponse.newBuilder().setMacValue(ByteString.copyFrom(macValue)).build();
} catch (GeneralSecurityException | InvalidProtocolBufferException e) {
response = ComputeMacResponse.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.ComputeMacResponse in project tink by google.
the class TestingServicesTest method computeMac_failsOnBadKeyset.
@Test
public void computeMac_failsOnBadKeyset() throws Exception {
byte[] badKeyset = "bad keyset".getBytes(UTF_8);
byte[] data = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
ComputeMacResponse compResponse = computeMac(macStub, badKeyset, data);
assertThat(compResponse.getErr()).isNotEmpty();
}
Aggregations