use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class JsonKeysetWriterTest method testWriteEncrypted_shouldWork.
private void testWriteEncrypted_shouldWork(KeysetHandle handle1) throws Exception {
// Encrypt the keyset with an AeadKey.
KeyTemplate masterKeyTemplate = AeadKeyTemplates.AES128_EAX;
Aead masterKey = Registry.getPrimitive(Registry.newKeyData(masterKeyTemplate));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
handle1.write(JsonKeysetWriter.withOutputStream(outputStream), masterKey);
KeysetHandle handle2 = KeysetHandle.read(JsonKeysetReader.withInputStream(new ByteArrayInputStream(outputStream.toByteArray())), masterKey);
assertKeysetHandle(handle1, handle2);
}
use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class KeysetHandleTest method testWriteEncrypted.
@Test
public void testWriteEncrypted() throws Exception {
KeysetHandle handle = KeysetHandle.generateNew(MacKeyTemplates.HMAC_SHA256_128BITTAG);
// Encrypt the keyset with an AeadKey.
KeyTemplate masterKeyTemplate = AeadKeyTemplates.AES128_EAX;
Aead masterKey = Registry.getPrimitive(Registry.newKeyData(masterKeyTemplate));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
KeysetWriter writer = BinaryKeysetWriter.withOutputStream(outputStream);
handle.write(writer, masterKey);
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
KeysetReader reader = BinaryKeysetReader.withInputStream(inputStream);
KeysetHandle handle2 = KeysetHandle.read(reader, masterKey);
assertEquals(handle.getKeyset(), handle2.getKeyset());
}
use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class JsonKeysetReaderTest method testRead_singleKey_shouldWork.
@Test
public void testRead_singleKey_shouldWork() throws Exception {
KeyTemplate template = MacKeyTemplates.HMAC_SHA256_128BITTAG;
KeysetHandle handle1 = KeysetHandle.generateNew(template);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CleartextKeysetHandle.write(handle1, JsonKeysetWriter.withOutputStream(outputStream));
KeysetHandle handle2 = CleartextKeysetHandle.read(JsonKeysetReader.withInputStream(new ByteArrayInputStream(outputStream.toByteArray())));
assertKeysetHandle(handle1, handle2);
}
use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class KeysetManagerTest method testRotate_bogusKeyTemplate_shouldThrowException.
@Test
public void testRotate_bogusKeyTemplate_shouldThrowException() throws Exception {
KeyTemplate bogus = TestUtil.createKeyTemplateWithNonExistingTypeUrl();
try {
KeysetManager.withEmptyKeyset().rotate(bogus);
fail("Expected GeneralSecurityException");
} catch (GeneralSecurityException e) {
TestUtil.assertExceptionContains(e, "No key manager found for key type");
}
}
use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class AesEaxKeyManagerTest method testNewKeyWithCorruptedFormat.
@Test
public void testNewKeyWithCorruptedFormat() throws Exception {
ByteString serialized = ByteString.copyFrom(new byte[128]);
KeyTemplate keyTemplate = KeyTemplate.newBuilder().setTypeUrl(AesEaxKeyManager.TYPE_URL).setValue(serialized).build();
AesEaxKeyManager keyManager = new AesEaxKeyManager();
try {
keyManager.newKey(serialized);
fail("Corrupted format, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
try {
keyManager.newKeyData(keyTemplate.getValue());
fail("Corrupted format, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
}
Aggregations