use of com.google.crypto.tink.proto.KeyData in project tink by google.
the class KeysetHandle method getPublicKeysetHandle.
/**
* If the managed keyset contains private keys, returns a {@link KeysetHandle} of the public keys.
*
* @throws GenernalSecurityException if the managed keyset is null or if it contains any
* non-private keys.
*/
public KeysetHandle getPublicKeysetHandle() throws GeneralSecurityException {
if (keyset == null) {
throw new GeneralSecurityException("cleartext keyset is not available");
}
Keyset.Builder keysetBuilder = Keyset.newBuilder();
for (Keyset.Key key : keyset.getKeyList()) {
KeyData keyData = createPublicKeyData(key.getKeyData());
keysetBuilder.addKey(Keyset.Key.newBuilder().mergeFrom(key).setKeyData(keyData).build());
}
keysetBuilder.setPrimaryKeyId(keyset.getPrimaryKeyId());
return new KeysetHandle(keysetBuilder.build());
}
use of com.google.crypto.tink.proto.KeyData in project tink by google.
the class KeyHandleTest method createFromKey_keyDataUnknown_shouldHaveSecret.
@Test
public void createFromKey_keyDataUnknown_shouldHaveSecret() throws Exception {
KeyTemplate kt = KeyTemplates.get("ED25519");
KeyData kd = KeyData.newBuilder().mergeFrom(Registry.newKeyData(kt)).setKeyMaterialType(KeyData.KeyMaterialType.UNKNOWN_KEYMATERIAL).build();
KeyHandle kh = KeyHandle.createFromKey(kd, kt.getOutputPrefixType());
assertThat(kh.hasSecret()).isTrue();
}
use of com.google.crypto.tink.proto.KeyData in project tink by google.
the class KeyHandleTest method generateNew_generatesDifferentKeys.
@Test
public void generateNew_generatesDifferentKeys() throws Exception {
KeyTemplate template = KeyTemplates.get("AES128_EAX");
Set<String> keys = new TreeSet<>();
int numKeys = 2;
for (int j = 0; j < numKeys; j++) {
KeyHandle handle = KeyHandle.generateNew(template);
ProtoKey protoKey = (ProtoKey) handle.getKey(SecretKeyAccess.insecureSecretAccess());
KeyData keyData = protoKey.getProtoKey();
AesEaxKey aesEaxKey = AesEaxKey.parseFrom(keyData.getValue(), ExtensionRegistryLite.getEmptyRegistry());
keys.add(aesEaxKey.getKeyValue().toStringUtf8());
}
assertThat(keys).hasSize(numKeys);
}
use of com.google.crypto.tink.proto.KeyData in project tink by google.
the class KeyHandleTest method createFromKey_keyDataAsymmetricPrivate_shouldHaveSecret.
@Test
public void createFromKey_keyDataAsymmetricPrivate_shouldHaveSecret() throws Exception {
KeyTemplate kt = KeyTemplates.get("ED25519");
KeyData kd = Registry.newKeyData(kt);
KeyHandle kh = KeyHandle.createFromKey(kd, kt.getOutputPrefixType());
assertThat(kh.hasSecret()).isTrue();
}
use of com.google.crypto.tink.proto.KeyData in project tink by google.
the class KeyHandleTest method createFromKey_keyDataSymmetric_shouldHaveSecret.
@Test
public void createFromKey_keyDataSymmetric_shouldHaveSecret() throws Exception {
KeyTemplate kt = KeyTemplates.get("AES128_EAX");
KeyData kd = Registry.newKeyData(kt);
KeyHandle kh = KeyHandle.createFromKey(kd, kt.getOutputPrefixType());
assertThat(kh.hasSecret()).isTrue();
}
Aggregations