ChecksumUtil { public String generateChecksum(File file) throws IOException { try (FileInputStream stream = new FileInputStream(file)) { return generateChecksum(stream); } } ChecksumUtil(Algorithm alg); String generateChecksum(File file); String generateChecksum(InputStream inStream); String generateChecksum(String string); String generateChecksumBase64(String string); static DigestInputStream wrapStream(InputStream inStream,
                                               Algorithm algorithm); static String getChecksum(DigestInputStream digestStream); static byte[] getChecksumBytes(DigestInputStream digestStream); static String checksumBytesToString(byte[] digestBytes); static byte[] hexStringToByteArray(String s); static String convertToBase64Encoding(String hexEncodedChecksum);  }
@Test public void testGenerateChecksum() { ChecksumUtil util; util = new ChecksumUtil(Algorithm.MD2); String md2 = util.generateChecksum(getStream(content)); util = new ChecksumUtil(Algorithm.MD5); String md5 = util.generateChecksum(getStream(content)); util = new ChecksumUtil(Algorithm.SHA_1); String sha1 = util.generateChecksum(getStream(content)); util = new ChecksumUtil(Algorithm.SHA_256); String sha256 = util.generateChecksum(getStream(content)); util = new ChecksumUtil(Algorithm.SHA_384); String sha384 = util.generateChecksum(getStream(content)); util = new ChecksumUtil(Algorithm.SHA_512); String sha512 = util.generateChecksum(getStream(content)); assertNotNull(md2); assertNotNull(md5); assertNotNull(sha1); assertNotNull(sha256); assertNotNull(sha384); assertNotNull(sha512); boolean diff0 = (md2 != md5 && md2 != sha1 && md2 != sha256 && md2 != sha384 && md2 != sha512); boolean diff1 = (md5 != sha1 && md5 != sha256 && md5 != sha384 && md5 != sha256); boolean diff2 = (sha1 != sha256 && sha1 != sha384 && sha1 != sha512); boolean diff3 = (sha256 != sha384 && sha256 != sha512); boolean diff4 = (sha384 != sha512); assertTrue(diff0); assertTrue(diff1); assertTrue(diff2); assertTrue(diff3); assertTrue(diff4); }
@Test public void testGetFileChecksum() throws Exception { File tempFile = File.createTempFile("checksum-util-test", "file"); Writer writer = new FileWriter(tempFile); writer.write(content); writer.close(); ChecksumUtil util = new ChecksumUtil(Algorithm.MD5); try { String fileMd5 = util.generateChecksum(tempFile); String streamMd5 = util.generateChecksum(getStream(content)); assertEquals(fileMd5, streamMd5); } finally { tempFile.delete(); } }
@Test public void testReusable() throws Exception { ChecksumUtil util = new ChecksumUtil(Algorithm.MD5); String checksum = util.generateChecksum(new ByteArrayInputStream("test".getBytes())); InputStream is = EasyMock.createMock(InputStream.class); EasyMock.expect(is.available()).andReturn(100).anyTimes(); EasyMock.expect(is.read((byte[]) EasyMock.anyObject())).andReturn(100).times(1); EasyMock.expect(is.read((byte[]) EasyMock.anyObject())).andThrow(new IOException("test exception")); EasyMock.replay(is); try { util.generateChecksum(is); Assert.fail("expected previous statement to throw exception"); } catch (Exception ex) { } EasyMock.verify(is); String checksum2 = util.generateChecksum(new ByteArrayInputStream("test".getBytes())); assertEquals("same stream, same checksum util instance should produce same results.", checksum, checksum2); }
@Test public void testCompareChecksumMethods() throws Exception { String data = "Survival’s research"; File tempFile = File.createTempFile("checksum-util-test", "file"); Writer writer = new OutputStreamWriter(new FileOutputStream(tempFile), "UTF-8"); writer.write(data); writer.close(); ChecksumUtil util = new ChecksumUtil(Algorithm.MD5); try { String fileMd5 = util.generateChecksum(tempFile); System.out.println("File MD5: " + fileMd5); assertEquals("c56e8f84a1b33dd5b9caedac1017f1b1", fileMd5); String stringMd5 = util.generateChecksum(data); System.out.println("String MD5: " + stringMd5); assertEquals("c56e8f84a1b33dd5b9caedac1017f1b1", stringMd5); assertEquals(fileMd5, stringMd5); } finally { tempFile.delete(); } }
@Test public void testNotThreadSafe() throws Exception { byte[] data = new byte[1024 * 1024]; ChecksumUtil util = new ChecksumUtil(Algorithm.MD5); String checksum = util.generateChecksum(new ByteArrayInputStream(data)); int count = 40; CountDownLatch latch = new CountDownLatch(count); AtomicInteger successes = new AtomicInteger(0); for (int i = 0; i < 40; i++) { new Thread(new Runnable() { @Override public void run() { try { if (util.generateChecksum(new ByteArrayInputStream(data)).equals(checksum)) { successes.incrementAndGet(); } } catch (Exception e) { } latch.countDown(); } }).start(); } assertTrue(latch.await(10, TimeUnit.SECONDS)); assertTrue(count != successes.get()); }