@Test public void createKey_multipleCallsCreateDifferentKeys() throws Exception { AesSivKeyFormat format = createAesSivKeyFormat(64); TreeSet<String> keys = new TreeSet<>(); KeyTypeManager.KeyFactory<AesSivKeyFormat, AesSivKey> factory = new AesSivKeyManager().keyFactory(); final int numKeys = 1000; for (int i = 0; i < numKeys; ++i) { keys.add(TestUtil.hexEncode(factory.createKey(format).toByteArray())); } assertThat(keys).hasSize(numKeys); }
@Override public KeyFactory<AesSivKeyFormat, AesSivKey> keyFactory() { return new KeyFactory<AesSivKeyFormat, AesSivKey>(AesSivKeyFormat.class) { @Override public void validateKeyFormat(AesSivKeyFormat format) throws GeneralSecurityException { if (format.getKeySize() != 64) { throw new InvalidAlgorithmParameterException( "invalid key size: " + format.getKeySize() + ". Valid keys must have 64 bytes."); } } @Override public AesSivKeyFormat parseKeyFormat(ByteString byteString) throws InvalidProtocolBufferException { return AesSivKeyFormat.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry()); } @Override public AesSivKey createKey(AesSivKeyFormat format) throws GeneralSecurityException { return AesSivKey.newBuilder() .setKeyValue(ByteString.copyFrom(Random.randBytes(format.getKeySize()))) .setVersion(getVersion()) .build(); } }; }
AesSivKeyManager extends KeyTypeManager<AesSivKey> { @Override public KeyFactory<AesSivKeyFormat, AesSivKey> keyFactory() { return new KeyFactory<AesSivKeyFormat, AesSivKey>(AesSivKeyFormat.class) { @Override public void validateKeyFormat(AesSivKeyFormat format) throws GeneralSecurityException { if (format.getKeySize() != 64) { throw new InvalidAlgorithmParameterException( "invalid key size: " + format.getKeySize() + ". Valid keys must have 64 bytes."); } } @Override public AesSivKeyFormat parseKeyFormat(ByteString byteString) throws InvalidProtocolBufferException { return AesSivKeyFormat.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry()); } @Override public AesSivKey createKey(AesSivKeyFormat format) throws GeneralSecurityException { return AesSivKey.newBuilder() .setKeyValue(ByteString.copyFrom(Random.randBytes(format.getKeySize()))) .setVersion(getVersion()) .build(); } }; } }
AesSivKeyManager extends KeyTypeManager<AesSivKey> { @Override public KeyFactory<AesSivKeyFormat, AesSivKey> keyFactory() { return new KeyFactory<AesSivKeyFormat, AesSivKey>(AesSivKeyFormat.class) { @Override public void validateKeyFormat(AesSivKeyFormat format) throws GeneralSecurityException { if (format.getKeySize() != 64) { throw new InvalidAlgorithmParameterException( "invalid key size: " + format.getKeySize() + ". Valid keys must have 64 bytes."); } } @Override public AesSivKeyFormat parseKeyFormat(ByteString byteString) throws InvalidProtocolBufferException { return AesSivKeyFormat.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry()); } @Override public AesSivKey createKey(AesSivKeyFormat format) throws GeneralSecurityException { return AesSivKey.newBuilder() .setKeyValue(ByteString.copyFrom(Random.randBytes(format.getKeySize()))) .setVersion(getVersion()) .build(); } }; } AesSivKeyManager(); }
AesSivKeyManager extends KeyTypeManager<AesSivKey> { @Override public KeyFactory<AesSivKeyFormat, AesSivKey> keyFactory() { return new KeyFactory<AesSivKeyFormat, AesSivKey>(AesSivKeyFormat.class) { @Override public void validateKeyFormat(AesSivKeyFormat format) throws GeneralSecurityException { if (format.getKeySize() != 64) { throw new InvalidAlgorithmParameterException( "invalid key size: " + format.getKeySize() + ". Valid keys must have 64 bytes."); } } @Override public AesSivKeyFormat parseKeyFormat(ByteString byteString) throws InvalidProtocolBufferException { return AesSivKeyFormat.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry()); } @Override public AesSivKey createKey(AesSivKeyFormat format) throws GeneralSecurityException { return AesSivKey.newBuilder() .setKeyValue(ByteString.copyFrom(Random.randBytes(format.getKeySize()))) .setVersion(getVersion()) .build(); } }; } AesSivKeyManager(); @Override String getKeyType(); @Override int getVersion(); @Override KeyMaterialType keyMaterialType(); @Override void validateKey(AesSivKey key); @Override AesSivKey parseKey(ByteString byteString); @Override KeyFactory<AesSivKeyFormat, AesSivKey> keyFactory(); static void register(boolean newKeyAllowed); static final KeyTemplate aes256SivTemplate(); static final KeyTemplate rawAes256SivTemplate(); }
AesSivKeyManager extends KeyTypeManager<AesSivKey> { @Override public KeyFactory<AesSivKeyFormat, AesSivKey> keyFactory() { return new KeyFactory<AesSivKeyFormat, AesSivKey>(AesSivKeyFormat.class) { @Override public void validateKeyFormat(AesSivKeyFormat format) throws GeneralSecurityException { if (format.getKeySize() != 64) { throw new InvalidAlgorithmParameterException( "invalid key size: " + format.getKeySize() + ". Valid keys must have 64 bytes."); } } @Override public AesSivKeyFormat parseKeyFormat(ByteString byteString) throws InvalidProtocolBufferException { return AesSivKeyFormat.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry()); } @Override public AesSivKey createKey(AesSivKeyFormat format) throws GeneralSecurityException { return AesSivKey.newBuilder() .setKeyValue(ByteString.copyFrom(Random.randBytes(format.getKeySize()))) .setVersion(getVersion()) .build(); } }; } AesSivKeyManager(); @Override String getKeyType(); @Override int getVersion(); @Override KeyMaterialType keyMaterialType(); @Override void validateKey(AesSivKey key); @Override AesSivKey parseKey(ByteString byteString); @Override KeyFactory<AesSivKeyFormat, AesSivKey> keyFactory(); static void register(boolean newKeyAllowed); static final KeyTemplate aes256SivTemplate(); static final KeyTemplate rawAes256SivTemplate();  }