diff --git a/src/lib.rs b/src/lib.rs index 17ff39e..07c2538 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ pub mod b64; +pub mod xor; diff --git a/src/xor.rs b/src/xor.rs new file mode 100644 index 0000000..beb6406 --- /dev/null +++ b/src/xor.rs @@ -0,0 +1,20 @@ +pub fn fixed(a: &str, b: &str) -> Result { + let a_decoded = a.chars().map(|c| c.to_digit(16).unwrap() as u16); + let b_decoded = b.chars().map(|c| c.to_digit(16).unwrap() as u16); + let xor = a_decoded.zip(b_decoded).map(|(a, b)| a ^ b as char); + String::from_iter(xor) +} + +#[cfg(test)] +mod tests { + use super::fixed; + + #[test] + fn cryptopals() { + let a = "1c0111001f010100061a024b53535009181c"; + let b = "686974207468652062756c6c277320657965"; + let ex_output = "746865206b696420646f6e277420706c6179"; + let output = fixed(a, b); + assert_eq!(output, ex_output); + } +}