AnonymizedOriginTracer extends MessageInterceptorAdapter { public static String getAnonymizedOrigin(InetAddress address) { synchronized (CLIENT_CACHE) { String id = CLIENT_CACHE.get(address); if (id == null) { byte[] raw = address.getAddress().clone(); try { if (HMAC == null) { byte[] mask = KEY.getEncoded(); for (int index = 0; index < raw.length; ++index) { raw[index] ^= mask[index]; } } else { HMAC.init(KEY); raw = HMAC.doFinal(raw); } } catch (InvalidKeyException e) { } id = StringUtil.byteArray2HexString(raw, StringUtil.NO_SEPARATOR, ID_LENGTH); CLIENT_CACHE.put(address, id); } return id; } } AnonymizedOriginTracer(String scheme);  AnonymizedOriginTracer(String scheme, long filterTimeout); @Override void receiveRequest(Request request); @Override void receiveEmptyMessage(EmptyMessage message); boolean log(Message message); static String getAnonymizedOrigin(InetAddress address);  }
@Test public void testAnonymizedAddressIdentity() { String hash1 = AnonymizedOriginTracer.getAnonymizedOrigin(address1); String hash2 = AnonymizedOriginTracer.getAnonymizedOrigin(address2); String hash3 = AnonymizedOriginTracer.getAnonymizedOrigin(address3); String hash4 = AnonymizedOriginTracer.getAnonymizedOrigin(address4); String hash5 = AnonymizedOriginTracer.getAnonymizedOrigin(address5); String hash1_2 = AnonymizedOriginTracer.getAnonymizedOrigin(address1_2); String hash2_2 = AnonymizedOriginTracer.getAnonymizedOrigin(address2); String hash3_2 = AnonymizedOriginTracer.getAnonymizedOrigin(address3); String hash4_2 = AnonymizedOriginTracer.getAnonymizedOrigin(address4); String hash5_2 = AnonymizedOriginTracer.getAnonymizedOrigin(address5); assertThat(hash1, is(hash1_2)); assertThat(hash2, is(hash2_2)); assertThat(hash3, is(hash3_2)); assertThat(hash4, is(hash4_2)); assertThat(hash5, is(hash5_2)); }
@Test public void testAnonymizedAddressDiffers() throws UnknownHostException { int differsCounter = 0; String hash1 = AnonymizedOriginTracer.getAnonymizedOrigin(address1); for (int tries = 0; tries < 100; ++tries) { ++rawAddress[0]; InetAddress differentAddress = InetAddress.getByAddress(rawAddress); String hash2 = AnonymizedOriginTracer.getAnonymizedOrigin(differentAddress); if (!hash1.equals(hash2)) { ++differsCounter; } } assertThat(differsCounter, is(greaterThan(95))); }