cryptopals-rust/set1/fixed_xor/src/main.rs

35 lines
917 B
Rust

#[macro_use]
extern crate log;
extern crate env_logger;
extern crate rustc_serialize;
use rustc_serialize::hex::{FromHex, ToHex};
use std::io::{BufRead, stdin};
struct Pairwise<T: Iterator>(T);
impl<T> Iterator for Pairwise<T> where T: Iterator {
type Item = (T::Item, T::Item);
fn next(&mut self) -> Option<Self::Item> {
if let (Some(fst), Some(snd)) = (self.0.next(), self.0.next()) {
Some((fst, snd))
} else {
None
}
}
}
fn main() {
env_logger::init().unwrap();
let stdin = stdin();
for (line1, line2) in Pairwise(stdin.lock().lines().filter_map(|x| x.ok())) {
match (line1.from_hex(), line2.from_hex()) {
(Ok(v1), Ok(v2)) => println!("{}", v1.iter().zip(v2).map(|(a, b)| a ^ b).collect::<Vec<u8>>().to_hex()),
(Err(e), _) => error!("{}", e),
(_, Err(e)) => error!("{}", e)
}
}
}