1
2pub fn xor(x: &[u8], y: &[u8], output: &mut [u8]) {
5 assert!( x.len() == y.len() && x.len() == output.len() );
6 for ((out, &x_i), &y_i) in output.iter_mut().zip(x.iter()).zip(y.iter()) {
7 *out = x_i ^ y_i;
8 }
9}
10
11pub fn xor_assign(a: &mut [u8], b: &[u8]) {
12 assert!( a.len() == b.len() );
13 for (a_i, &b_i) in a.iter_mut().zip(b.iter()) {
14 *a_i ^= b_i;
15 }
16}
17
18#[cfg(test)]
19mod tests {
20 use super::*;
21
22 #[test]
23 fn simple_xor_test() {
24 let x = [0x8e, 0xc6, 0xcb, 0x71];
25 let y = [0x2e, 0x51, 0xa9, 0x21];
26 let mut output = [1u8; 4];
27 xor(&x, &y, &mut output);
28 let want = hex::decode("a0976250").unwrap();
29 assert_eq!(output, want.as_slice());
30 }
31
32 #[test]
33 fn simple_xor_assign_test() {
34 let mut x = [0x8e, 0xc6, 0xcb, 0x71];
35 let y = [0x2e, 0x51, 0xa9, 0x21];
36 xor_assign(&mut x, &y);
37 let want = hex::decode("a0976250").unwrap();
38 assert_eq!(x, want.as_slice());
39 }
40}