SipHashInline { public static long hash24(byte[] key, byte[] data, int offset, int len) { return hash24(key, data, offset, len, false); }  static long hash24(byte[] key, byte[] data, int offset, int len); static long hash24(byte[] key, byte[] data, int offset, int len, boolean reversed); static long hash24(byte[] key, byte[] data); static long hash24(long k0, long k1, byte[] data, int offset, int len); static long hash24(long k0, long k1, byte[] data, int offset, int len, boolean reversed); static long hash24_palindromic(long k0, long k1, byte[] data, int offset, int len); static long hash24_palindromic(byte[] key, byte[] data, int offset, int len); static long hash24_palindromic(long k0, long k1, byte[] data); static long hash24_palindromic(byte[] key, byte[] data); static long[] getKey(byte[] key);  }
@Test public void testVectors() { long k0 = 0x0706050403020100L; long k1 = 0x0f0e0d0c0b0a0908L; for (int i = 0; i < EXPECTED.length; ++i) { byte[] msg = new byte[i]; for (int j = 0; j < i; ++j) { msg[j] = (byte) j; } Assert.assertEquals(EXPECTED[i], SipHashInline.hash24(k0, k1, msg, 0, msg.length)); } }
@Test public void testReversedHashes() { long k0 = 0x0706050403020100L; long k1 = 0x0f0e0d0c0b0a0908L; byte[] msg = "Too many secrets, Marty!".getBytes(StandardCharsets.UTF_8); byte[] rmsg = "!ytraM ,sterces ynam ooT".getBytes(StandardCharsets.UTF_8); long hash24 = SipHashInline.hash24(k0,k1,msg,0,msg.length,false); double ms = 0D; byte[] key = new byte[16]; for (int i = 0; i < 1000000; i++) { long nano = System.nanoTime(); Assert.assertEquals(SipHashInline.hash24(k0,k1,msg,0,msg.length,true),SipHashInline.hash24(k0,k1,rmsg,0,rmsg.length,false)); Assert.assertEquals(SipHashInline.hash24(k0,k1,msg,0,msg.length,false),SipHashInline.hash24(k0,k1,rmsg,0,rmsg.length,true)); ms += (System.nanoTime() - nano); } System.out.println(ms/1000000.0D); }
@Test public void testCollission() { byte[] b1 = new byte[] { 109, -45, -99, -85, -72, 37, -51, 120, -56, -10, -17, -53, -83, 84, -127, 67 }; byte[] b2 = new byte[] { 109, -45, -99, -85, -72, 37, -51, 120, -56, 80, 111, 67, -59, 92, 100, 2 }; long h1 = SipHashInline.hash24(new byte[16], b1); long h2 = SipHashInline.hash24(new byte[16], b2); Assert.assertTrue(h1 != h2); }
@Test public void testHash24_dbl_perf() { Random r = new Random(); byte[] data = new byte[1000000]; r.nextBytes(data); long k0 = r.nextLong(); long k1 = r.nextLong(); long nano = System.nanoTime(); for (int i = 0; i < 1000; i++) { byte[] dbl = new byte[data.length * 2]; for (int j = 0; j < data.length; j++) { dbl[j] = data[j]; dbl[data.length + j] = data[(data.length - 1) - j]; } long hash = SipHashInline.hash24(k0, k1, dbl, 0, dbl.length); } System.out.println((System.nanoTime() - nano) / 1000000.0D); }
@Test public void testPerf() { byte[] buf = new byte[256]; SecureRandom sr = new SecureRandom(); sr.nextBytes(buf); long nano = System.nanoTime(); for (int i = 0; i < 1000; i++) { long hash = SipHashInline.hash24(0L, 1L, buf, 0, 256); } System.out.println((System.nanoTime() - nano) / 1000000.0D); }