ByteArrayHelper { public static void xor(List<byte[]> vector1, List<byte[]> vector2) { if (vector1.size() != vector2.size()) { throw new IllegalArgumentException("The vectors are not of equal length"); } for (int i = 0; i < vector1.size(); i++) { xor(vector1.get(i), vector2.get(i)); } } private  ByteArrayHelper(); static boolean getBit(byte[] input, int index); static void setBit(byte[] input, int index, boolean choice); static void xor(List<byte[]> vector1, List<byte[]> vector2); static void xor(byte[] arr1, byte[] arr2); static void shiftArray(byte[] input, byte[] output, int positions);  }
@Test public void testXor() { byte[] arr1 = { (byte) 0x00, (byte) 0x02, (byte) 0xFF }; byte[] arr2 = { (byte) 0xF0, (byte) 0x02, (byte) 0xF0 }; ByteArrayHelper.xor(arr1, arr2); assertEquals((byte) 0xF0, arr1[0]); assertEquals((byte) 0x00, arr1[1]); assertEquals((byte) 0x0F, arr1[2]); }
@Test public void testXorList() { byte[] arr1 = { (byte) 0x00, (byte) 0x02, (byte) 0xFF }; byte[] arr2 = { (byte) 0xF0, (byte) 0x02, (byte) 0xF0 }; List<byte[]> list1 = new ArrayList<>(2); List<byte[]> list2 = new ArrayList<>(2); list1.add(arr1); list1.add(arr2); list2.add(arr2.clone()); list2.add(arr1.clone()); ByteArrayHelper.xor(list1, list2); assertEquals((byte) 0xF0, list1.get(0)[0]); assertEquals((byte) 0x00, list1.get(0)[1]); assertEquals((byte) 0x0F, list1.get(0)[2]); assertEquals((byte) 0xF0, list1.get(1)[0]); assertEquals((byte) 0x00, list1.get(1)[1]); assertEquals((byte) 0x0F, list1.get(1)[2]); }
@Test public void testIllegalXor() { byte[] arr1 = new byte[34]; byte[] arr2 = new byte[35]; boolean thrown = false; try { ByteArrayHelper.xor(arr1, arr2); } catch (IllegalArgumentException e) { assertEquals("The byte arrays are not of equal length", e.getMessage()); thrown = true; } assertEquals(thrown, true); }
@Test public void testIllegalXorList() { List<byte[]> list1 = new ArrayList<>(); List<byte[]> list2 = new ArrayList<>(); list1.add(new byte[12]); list1.add(new byte[15]); list2.add(new byte[12]); list2.add(new byte[15]); list2.add(new byte[1]); boolean thrown = false; try { ByteArrayHelper.xor(list1, list2); } catch (IllegalArgumentException e) { assertEquals("The vectors are not of equal length", e.getMessage()); thrown = true; } assertEquals(thrown, true); thrown = false; list1.add(new byte[2]); try { ByteArrayHelper.xor(list1, list2); } catch (IllegalArgumentException e) { assertEquals("The byte arrays are not of equal length", e.getMessage()); thrown = true; } assertEquals(thrown, true); }