Subtles { public static boolean secureCompare(final byte[] a, final byte[] b) { if (a.length != b.length) { return false; } int match = 0; for (int i = 0; i < a.length; i++) { match = match | a[i] ^ b[i]; } return (match == 0); } private  Subtles(); static boolean secureCompare(final byte[] a, final byte[] b); static boolean secureCompare(final char[] a, final char[] b); static boolean secureCompare(final String a, final String b);  }
@Test public void itReturnsFalseWhenBytesDontMatch() throws Exception { byte[] a = Hex.decodeHex("000A00BB".toCharArray()); byte[] b = Hex.decodeHex("000A01BB".toCharArray()); assertThat(Subtles.secureCompare(a, b)).isFalse(); }
@Test public void itReturnsFalseWithBytesOfDifferentLengths() throws Exception { byte[] a = Hex.decodeHex("000A00BB".toCharArray()); byte[] b = Hex.decodeHex("000A00".toCharArray()); assertThat(Subtles.secureCompare(a, b)).isFalse(); }
@Test public void itReturnsTrueWhenBytesMatch() throws Exception { byte[] a = Hex.decodeHex("030A01BF".toCharArray()); byte[] b = Hex.decodeHex("030A01BF".toCharArray()); assertThat(Subtles.secureCompare(a, b)).isTrue(); }