### Question:
HashUtil { public static byte[] hash(byte[] data) { try { MessageDigest digest = MessageDigest.getInstance(HASH_ALGORITHM); digest.update(data, 0, data.length); return digest.digest(); } catch (NoSuchAlgorithmException e) { logger.error("Invalid hash algorithm {}", HASH_ALGORITHM, e); return new byte[0]; } } private  HashUtil(); static byte[] hash(byte[] data); static byte[] hash(File file); static boolean compare(File file, byte[] expectedHash); static boolean compare(byte[] actual, byte[] expected);  }### Answer:
@Test public void hashDataTest() { String data = randomString(1000); byte[] hash = HashUtil.hash(data.getBytes()); assertNotNull(hash); assertEquals(new String(hash), new String(HashUtil.hash(data.getBytes()))); String data2 = randomString(1000); assertNotEquals(data, data2); assertNotEquals(new String(hash), new String(HashUtil.hash(data2.getBytes()))); }
@Test public void hashExampleDataTest() { final String expected = "uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="; String data = "hello world"; byte[] hash = HashUtil.hash(data.getBytes()); String result = new String(Base64.encode(hash)); assertEquals(expected, result); }
@Test public void hashStreamTest() throws IOException { String data = randomString(5 * 1024); File file = new File(System.getProperty("java.io.tmpdir"), randomString()); FileUtils.writeStringToFile(file, data); byte[] hash = HashUtil.hash(file); assertNotNull(hash); assertEquals(new String(hash), new String(HashUtil.hash(file))); String data2 = randomString(1000); assertNotEquals(data, data2); assertNotEquals(new String(hash), new String(HashUtil.hash(data2.getBytes()))); }
@Test public void hashStreamExampleDataTest() throws IOException { final String expected = "uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="; String data = "hello world"; File file = new File(FileUtils.getTempDirectory(), randomString()); FileUtils.writeStringToFile(file, data); byte[] hash = HashUtil.hash(file); String result = new String(Base64.encode(hash)); assertEquals(expected, result); }