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();  }
@Test public void validateKeyFormat_empty() throws Exception { try { new AesSivKeyManager().keyFactory().validateKeyFormat(AesSivKeyFormat.getDefaultInstance()); fail(); } catch (GeneralSecurityException e) { } }
@Test public void validateKeyFormat_checkAllLengths() throws Exception { AesSivKeyManager manager = new AesSivKeyManager(); for (int i = 0; i < 100; i++) { if (i == 64) { manager.keyFactory().validateKeyFormat(createAesSivKeyFormat(i)); } else { try { manager.keyFactory().validateKeyFormat(createAesSivKeyFormat(i)); fail(); } catch (GeneralSecurityException e) { } } } }
@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); }