Checksum { public static String checksum(byte[] data) { UUID uuid = UUID.nameUUIDFromBytes(data); ByteBuffer buffer = ByteBuffer.allocate(16); buffer.putLong(uuid.getMostSignificantBits()); buffer.putLong(uuid.getLeastSignificantBits()); String encoded = encode(buffer.array()); return encoded + hash(encoded); }  static String checksum(byte[] data); static String checksum(String in); static boolean validateChecksum(String encoded);  }
@Test public void testChecksum() { for (String test : SAMPLE_DATA) { byte[] src = test.getBytes(); String encoded = Checksum.encode(src); byte[] decoded = Checksum.decode(encoded); Assert.assertTrue(String.format("String(%s) decoded result not equals!", src), Arrays.equals(src, decoded)); } for (String test : SAMPLE_DATA) { byte[] src = test.getBytes(); String ck = Checksum.checksum(src); Assert.assertTrue(String.format("String(%s) checksum validation falied!", ck), Checksum.validateChecksum(ck)); } for (int i = 0; i < 10000; ++i) { int size = (int) (Math.random() * 1000); byte[] buf = new byte[size]; for (int j = 0; j < size; ++j) { buf[j] = (byte) (Math.random() * 256); } String ck = Checksum.checksum(buf); Assert.assertTrue(String.format("String(%s) checksum validation falied!", ck), Checksum.validateChecksum(ck)); } Assert.assertTrue(Checksum.decode(null).length == 0); Assert.assertFalse(Checksum.validateChecksum(null)); Assert.assertFalse(Checksum.validateChecksum("abc")); Assert.assertFalse(Checksum.validateChecksum("abcabcabcabcabcabcabcabcab!")); Assert.assertFalse(Checksum.validateChecksum("abcabcabcabcabcabcabcabcabc")); }