1.2: Finish implementation of fixed XOR
This commit is contained in:
parent
af8c16dcc4
commit
493d4beb3b
1 changed files with 13 additions and 6 deletions
19
src/xor.rs
19
src/xor.rs
|
@ -1,8 +1,15 @@
|
||||||
pub fn fixed(a: &str, b: &str) -> Result<String, String> {
|
use hex::{AsHexBytes, HexDigest};
|
||||||
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);
|
#[derive(Debug)]
|
||||||
let xor = a_decoded.zip(b_decoded).map(|(a, b)| a ^ b as char);
|
pub enum FixedXORError {
|
||||||
String::from_iter(xor)
|
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)]
|
#[cfg(test)]
|
||||||
|
@ -15,6 +22,6 @@ mod tests {
|
||||||
let b = "686974207468652062756c6c277320657965";
|
let b = "686974207468652062756c6c277320657965";
|
||||||
let ex_output = "746865206b696420646f6e277420706c6179";
|
let ex_output = "746865206b696420646f6e277420706c6179";
|
||||||
let output = fixed(a, b);
|
let output = fixed(a, b);
|
||||||
assert_eq!(output, ex_output);
|
assert_eq!(output.unwrap(), ex_output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue