HashUtil { public static byte[] hash(String algorithm, String str) throws NoSuchAlgorithmException { if (algorithm == null || str == null) { return null; } MessageDigest md = MessageDigest.getInstance(algorithm.toUpperCase()); return md.digest(str.getBytes()); }  static byte[] hash(String algorithm, String str); static byte[] hashMD5(String str); static byte[] hashSHA1(String str);  }
@Test public void testHash01() throws Exception { String paramAlgorithm = "MD5"; String paramStr = "abc"; byte[] resultHashValue = HashUtil.hash(paramAlgorithm, paramStr); assertTrue( MessageDigest.isEqual( MessageDigest.getInstance("MD5").digest("abc".getBytes()), resultHashValue)); }
@Test public void testHash02() throws Exception { String paramAlgorithm = "MD5"; String paramStr = "あいう"; byte[] resultHashValue = HashUtil.hash(paramAlgorithm, paramStr); assertTrue( MessageDigest.isEqual( MessageDigest.getInstance("MD5").digest("あいう".getBytes()), resultHashValue)); }
@Test public void testHash03() throws Exception { String paramAlgorithm = "MD5"; String paramStr = ""; byte[] resultHashValue = HashUtil.hash(paramAlgorithm, paramStr); assertTrue( MessageDigest.isEqual( MessageDigest.getInstance("MD5").digest("".getBytes()), resultHashValue)); }
@Test public void testHash04() throws Exception { String paramAlgorithm = "md5"; String paramStr = ""; byte[] resultHashValue = HashUtil.hash(paramAlgorithm, paramStr); assertTrue( MessageDigest.isEqual( MessageDigest.getInstance("MD5").digest("".getBytes()), resultHashValue)); }
@Test public void testHash05() throws Exception { String paramAlgorithm = "md5"; String paramStr = null; byte[] resultHashValue = HashUtil.hash(paramAlgorithm, paramStr); assertNull(resultHashValue); }
@Test public void testHash06() throws Exception { String paramAlgorithm = "NoSuchAlgorithm"; String paramStr = "abc"; try { HashUtil.hash(paramAlgorithm, paramStr); fail(); } catch (NoSuchAlgorithmException nsae) { return; } }
@Test public void testHash07() throws Exception { String paramAlgorithm = ""; String paramStr = "abc"; try { HashUtil.hash(paramAlgorithm, paramStr); fail(); } catch (NoSuchAlgorithmException nsae) { return; } }
@Test public void testHash08() throws Exception { String paramAlgorithm = null; String paramStr = "abc"; byte[] resultHashValue = HashUtil.hash(paramAlgorithm, paramStr); assertNull(resultHashValue); }