From 493d4beb3bd1669ffb545e134a305a228ab6f215 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sun, 25 Mar 2018 18:51:01 -0400 Subject: [PATCH] 1.2: Finish implementation of fixed XOR --- src/xor.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/xor.rs b/src/xor.rs index beb6406..4548d75 100644 --- a/src/xor.rs +++ b/src/xor.rs @@ -1,8 +1,15 @@ -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) +use hex::{AsHexBytes, HexDigest}; + +#[derive(Debug)] +pub enum FixedXORError { + Bad, +} + +pub fn fixed(a: &str, b: &str) -> Result { + let a_decoded = a.hex_bytes().valid(); + let b_decoded = b.hex_bytes().valid(); + let xor: Vec = a_decoded.zip(b_decoded).map(|(x, y)| x ^ y).collect(); + Ok(xor.hex_digest()) } #[cfg(test)] @@ -15,6 +22,6 @@ mod tests { let b = "686974207468652062756c6c277320657965"; let ex_output = "746865206b696420646f6e277420706c6179"; let output = fixed(a, b); - assert_eq!(output, ex_output); + assert_eq!(output.unwrap(), ex_output); } }