use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class KeyHandleTest method generateNew_shouldWork.
@Test
public void generateNew_shouldWork() throws Exception {
KeyTemplate template = KeyTemplates.get("AES128_EAX");
KeyHandle handle = KeyHandle.generateNew(template);
ProtoKey protoKey = (ProtoKey) handle.getKey(SecretKeyAccess.insecureSecretAccess());
expect.that(protoKey.getOutputPrefixType()).isEqualTo(KeyTemplate.OutputPrefixType.TINK);
expect.that(protoKey.hasSecret()).isTrue();
KeyData keyData = protoKey.getProtoKey();
expect.that(keyData.getTypeUrl()).isEqualTo(template.getTypeUrl());
AesEaxKeyFormat aesEaxKeyFormat = AesEaxKeyFormat.parseFrom(template.getValue(), ExtensionRegistryLite.getEmptyRegistry());
AesEaxKey aesEaxKey = AesEaxKey.parseFrom(keyData.getValue(), ExtensionRegistryLite.getEmptyRegistry());
expect.that(aesEaxKey.getKeyValue().size()).isEqualTo(aesEaxKeyFormat.getKeySize());
}
use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class KeyHandleTest method createFromKey_keyDataRemote_shouldNotHaveSecret.
@Test
public void createFromKey_keyDataRemote_shouldNotHaveSecret() throws Exception {
KeyTemplate kt = KeyTemplates.get("ED25519");
KeyData kd = KeyData.newBuilder().mergeFrom(Registry.newKeyData(kt)).setKeyMaterialType(KeyData.KeyMaterialType.REMOTE).build();
KeyHandle kh = KeyHandle.createFromKey(kd, kt.getOutputPrefixType());
assertThat(kh.hasSecret()).isFalse();
}
use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class KeyHandleTest method generateNew_compareWith_createFromKeyViaProtoKey_shouldBeEqual.
@Test
public void generateNew_compareWith_createFromKeyViaProtoKey_shouldBeEqual() throws Exception {
KeyTemplate template = KeyTemplates.get("AES128_EAX");
KeyData keyData = Registry.newKeyData(template);
ProtoKey protoKey = new ProtoKey(keyData, template.getOutputPrefixType());
KeyHandle handle1 = KeyHandle.generateNew(template);
KeyHandle handle2 = KeyHandle.createFromKey(protoKey, SecretKeyAccess.insecureSecretAccess());
expect.that(handle1.getStatus()).isEqualTo(handle2.getStatus());
ProtoKey outputProtoKey1 = (ProtoKey) handle1.getKey(SecretKeyAccess.insecureSecretAccess());
ProtoKey outputProtoKey2 = (ProtoKey) handle2.getKey(SecretKeyAccess.insecureSecretAccess());
expect.that(outputProtoKey1.getOutputPrefixType()).isEqualTo(outputProtoKey2.getOutputPrefixType());
expect.that(handle1.hasSecret()).isEqualTo(handle2.hasSecret());
}
use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class KeyHandleTest method generateNew_unregisteredTypeUrl_shouldThrow.
@Test
public void generateNew_unregisteredTypeUrl_shouldThrow() throws Exception {
String typeUrl = "testNewKeyDataTypeUrl";
ByteString keyformat = ByteString.copyFromUtf8("testNewKeyDataKeyFormat");
com.google.crypto.tink.KeyTemplate keyTemplate = com.google.crypto.tink.KeyTemplate.create(typeUrl, keyformat.toByteArray(), OutputPrefixType.TINK);
assertThrows(GeneralSecurityException.class, () -> KeyHandle.generateNew(keyTemplate));
}
use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class ProtoKeyTest method testProtoKey_keyDataUNKNOWN_shouldHaveSecret.
@Test
public void testProtoKey_keyDataUNKNOWN_shouldHaveSecret() throws GeneralSecurityException {
KeyTemplate kt = KeyTemplates.get("ED25519");
KeyData kd = KeyData.newBuilder().mergeFrom(Registry.newKeyData(kt)).setKeyMaterialType(KeyData.KeyMaterialType.UNKNOWN_KEYMATERIAL).build();
ProtoKey pk = new ProtoKey(kd, kt.getOutputPrefixType());
assertThat(pk.getProtoKey()).isEqualTo(kd);
assertThat(pk.getOutputPrefixType()).isEqualTo(kt.getOutputPrefixType());
assertThat(pk.hasSecret()).isTrue();
}
Aggregations