HmacKeyManager extends KeyTypeManager<HmacKey> { @Override public void validateKey(HmacKey key) throws GeneralSecurityException { Validators.validateVersion(key.getVersion(), getVersion()); if (key.getKeyValue().size() < MIN_KEY_SIZE_IN_BYTES) { throw new GeneralSecurityException("key too short"); } validateParams(key.getParams()); } HmacKeyManager(); @Override String getKeyType(); @Override int getVersion(); @Override KeyMaterialType keyMaterialType(); @Override void validateKey(HmacKey key); @Override HmacKey parseKey(ByteString byteString); @Override KeyFactory<HmacKeyFormat, HmacKey> keyFactory(); static void register(boolean newKeyAllowed); static final KeyTemplate hmacSha256HalfDigestTemplate(); static final KeyTemplate hmacSha256Template(); static final KeyTemplate hmacSha512HalfDigestTemplate(); static final KeyTemplate hmacSha512Template();  }
@Test public void createKey_valid() throws Exception { manager.validateKey(factory.createKey(makeHmacKeyFormat(16, 10, HashType.SHA1))); manager.validateKey(factory.createKey(makeHmacKeyFormat(16, 20, HashType.SHA1))); manager.validateKey(factory.createKey(makeHmacKeyFormat(16, 10, HashType.SHA256))); manager.validateKey(factory.createKey(makeHmacKeyFormat(16, 32, HashType.SHA256))); manager.validateKey(factory.createKey(makeHmacKeyFormat(16, 10, HashType.SHA512))); manager.validateKey(factory.createKey(makeHmacKeyFormat(16, 64, HashType.SHA512))); }
@Test public void validateKey_wrongVersion_throws() throws Exception { HmacKey validKey = factory.createKey(makeHmacKeyFormat(16, 10, HashType.SHA1)); try { manager.validateKey(HmacKey.newBuilder(validKey).setVersion(1).build()); fail(); } catch (GeneralSecurityException e) { } }
@Test public void validateKey_notValid_throws() throws Exception { HmacKey validKey = factory.createKey(makeHmacKeyFormat(16, 10, HashType.SHA1)); try { manager.validateKey( HmacKey.newBuilder(validKey) .setKeyValue(ByteString.copyFrom(Random.randBytes(15))) .build()); fail(); } catch (GeneralSecurityException e) { } try { manager .validateKey( HmacKey.newBuilder(validKey) .setParams(HmacParams.newBuilder(validKey.getParams()).setTagSize(0).build()) .build()); fail(); } catch (GeneralSecurityException e) { } try { manager .validateKey( HmacKey.newBuilder(validKey) .setParams(HmacParams.newBuilder(validKey.getParams()).setTagSize(9).build()) .build()); fail(); } catch (GeneralSecurityException e) { } try { manager .validateKey( HmacKey.newBuilder(validKey) .setParams(HmacParams.newBuilder(validKey.getParams()).setTagSize(21).build()) .build()); fail(); } catch (GeneralSecurityException e) { } try { manager .validateKey( HmacKey.newBuilder(validKey) .setParams(HmacParams.newBuilder(validKey.getParams()).setTagSize(32).build()) .build()); fail(); } catch (GeneralSecurityException e) { } }