@Test public void testGetPublicKeysetHandle() throws Exception { KeysetHandle privateHandle = KeysetHandle.generateNew(SignatureKeyTemplates.ECDSA_P256); KeyData privateKeyData = privateHandle.getKeyset().getKey(0).getKeyData(); EcdsaPrivateKey privateKey = EcdsaPrivateKey.parseFrom( privateKeyData.getValue(), ExtensionRegistryLite.getEmptyRegistry()); KeysetHandle publicHandle = privateHandle.getPublicKeysetHandle(); assertEquals(1, publicHandle.getKeyset().getKeyCount()); assertEquals( privateHandle.getKeyset().getPrimaryKeyId(), publicHandle.getKeyset().getPrimaryKeyId()); KeyData publicKeyData = publicHandle.getKeyset().getKey(0).getKeyData(); assertEquals(SignatureConfig.ECDSA_PUBLIC_KEY_TYPE_URL, publicKeyData.getTypeUrl()); assertEquals(KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC, publicKeyData.getKeyMaterialType()); assertArrayEquals( privateKey.getPublicKey().toByteArray(), publicKeyData.getValue().toByteArray()); PublicKeySign signer = PublicKeySignFactory.getPrimitive(privateHandle); PublicKeyVerify verifier = PublicKeyVerifyFactory.getPrimitive(publicHandle); byte[] message = Random.randBytes(20); try { verifier.verify(signer.sign(message), message); } catch (GeneralSecurityException e) { fail("Should not fail: " + e); } }
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()); }
KeysetHandle { 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()); } }
KeysetHandle { 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()); } private  KeysetHandle(Keyset keyset); }
KeysetHandle { 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()); } private  KeysetHandle(Keyset keyset); KeysetInfo getKeysetInfo(); @Deprecated static final KeysetHandle generateNew(com.google.crypto.tink.proto.KeyTemplate keyTemplate); static final KeysetHandle generateNew(KeyTemplate keyTemplate); static final KeysetHandle read(KeysetReader reader, Aead masterKey); static final KeysetHandle readNoSecret(KeysetReader reader); static final KeysetHandle readNoSecret(final byte[] serialized); void write(KeysetWriter keysetWriter, Aead masterKey); void writeNoSecret(KeysetWriter writer); KeysetHandle getPublicKeysetHandle(); @Override String toString(); static void assertEnoughKeyMaterial(Keyset keyset); static void assertEnoughEncryptedKeyMaterial(EncryptedKeyset keyset); P getPrimitive(Class<P> targetClassObject); P getPrimitive(KeyManager<P> customKeyManager, Class<P> targetClassObject); }
KeysetHandle { 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()); } private  KeysetHandle(Keyset keyset); KeysetInfo getKeysetInfo(); @Deprecated static final KeysetHandle generateNew(com.google.crypto.tink.proto.KeyTemplate keyTemplate); static final KeysetHandle generateNew(KeyTemplate keyTemplate); static final KeysetHandle read(KeysetReader reader, Aead masterKey); static final KeysetHandle readNoSecret(KeysetReader reader); static final KeysetHandle readNoSecret(final byte[] serialized); void write(KeysetWriter keysetWriter, Aead masterKey); void writeNoSecret(KeysetWriter writer); KeysetHandle getPublicKeysetHandle(); @Override String toString(); static void assertEnoughKeyMaterial(Keyset keyset); static void assertEnoughEncryptedKeyMaterial(EncryptedKeyset keyset); P getPrimitive(Class<P> targetClassObject); P getPrimitive(KeyManager<P> customKeyManager, Class<P> targetClassObject);  }