MessageDigestUtil { public static MessageDigest createMessageDigest(String hashAlgorithm) { try { switch (hashAlgorithm) { case "S256": return MessageDigest.getInstance("SHA-256"); case "S384": return MessageDigest.getInstance("SHA-384"); case "S512": return MessageDigest.getInstance("SHA-512"); default: return MessageDigest.getInstance(hashAlgorithm); } } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException(e); } } private  MessageDigestUtil(); static MessageDigest createMessageDigest(String hashAlgorithm); static MessageDigest createSHA256();  }
@Test void createMessageDigest_test() { MessageDigest s256 = MessageDigestUtil.createMessageDigest("S256"); MessageDigest s384 = MessageDigestUtil.createMessageDigest("S384"); MessageDigest s512 = MessageDigestUtil.createMessageDigest("S512"); MessageDigest sha256 = MessageDigestUtil.createMessageDigest("SHA-256"); assertAll( () -> assertThat(s256.getAlgorithm()).isEqualTo("SHA-256"), () -> assertThat(s384.getAlgorithm()).isEqualTo("SHA-384"), () -> assertThat(s512.getAlgorithm()).isEqualTo("SHA-512"), () -> assertThat(sha256.getAlgorithm()).isEqualTo("SHA-256") ); }
@Test void createMessageDigest_test_with_wrong_arg() { assertThrows(IllegalArgumentException.class, () -> MessageDigestUtil.createMessageDigest("wrong-arg") ); }