JwtHmacKeyManager extends KeyTypeManager<JwtHmacKey> { @Override public void validateKey(JwtHmacKey key) throws GeneralSecurityException { Validators.validateVersion(key.getVersion(), getVersion()); if (key.getKeyValue().size() < MIN_KEY_SIZE_IN_BYTES) { throw new GeneralSecurityException("key too short"); } } JwtHmacKeyManager(); @Override String getKeyType(); @Override int getVersion(); @Override KeyMaterialType keyMaterialType(); @Override void validateKey(JwtHmacKey key); @Override JwtHmacKey parseKey(ByteString byteString); @Override KeyFactory<JwtHmacKeyFormat, JwtHmacKey> keyFactory(); static void register(boolean newKeyAllowed); static final KeyTemplate hs256Template(); static final KeyTemplate hs384Template(); static final KeyTemplate hs512Template();  }
@Test public void createKey_valid() throws Exception { manager.validateKey(factory.createKey(makeJwtHmacKeyFormat(32, HashType.SHA256))); manager.validateKey(factory.createKey(makeJwtHmacKeyFormat(32, HashType.SHA256))); manager.validateKey(factory.createKey(makeJwtHmacKeyFormat(32, HashType.SHA512))); }
@Test public void validateKey_wrongVersion_throws() throws Exception { JwtHmacKey validKey = factory.createKey(makeJwtHmacKeyFormat(32, HashType.SHA256)); try { manager.validateKey(JwtHmacKey.newBuilder(validKey).setVersion(1).build()); fail(); } catch (GeneralSecurityException e) { } }
@Test public void validateKey_notValid_throws() throws Exception { JwtHmacKey validKey = factory.createKey(makeJwtHmacKeyFormat(32, HashType.SHA256)); try { manager.validateKey( JwtHmacKey.newBuilder(validKey) .setKeyValue(ByteString.copyFrom(Random.randBytes(31))) .build()); fail(); } catch (GeneralSecurityException e) { } }