1.2: Initial broken implementation of fixed XOR

This commit is contained in:
Eryn Wells 2018-03-25 16:42:59 -04:00
parent 36e06e5570
commit d7a94df135
2 changed files with 21 additions and 0 deletions

View file

@ -1 +1,2 @@
pub mod b64;
pub mod xor;

20
src/xor.rs Normal file
View file

@ -0,0 +1,20 @@
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)
}
#[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);
}
}