1.2: Finish implementation of fixed XOR

This commit is contained in:
Eryn Wells 2018-03-25 18:51:01 -04:00
parent af8c16dcc4
commit 493d4beb3b

View file

@ -1,8 +1,15 @@
pub fn fixed(a: &str, b: &str) -> Result<String, String> {
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<String, FixedXORError> {
let a_decoded = a.hex_bytes().valid();
let b_decoded = b.hex_bytes().valid();
let xor: Vec<u8> = 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);
}
}