Working on XOR; commented a bunch of stuff out

This commit is contained in:
Eryn Wells 2018-03-31 14:28:25 -07:00
parent 171b2942f3
commit 804902efc9
2 changed files with 61 additions and 45 deletions

View file

@ -1,3 +1,3 @@
pub mod b64; pub mod b64;
pub mod xor;
pub mod hex; pub mod hex;
pub mod xor;

View file

@ -1,49 +1,65 @@
use hex::{AsHexBytes, HexDigest}; use std::iter::{Iterator, Map, Zip};
#[derive(Debug)] //#[derive(Debug)]
pub enum FixedXORError { //pub enum FixedXORError {
Bad, // Bad,
//}
pub type FixedXOR<T, F> = Map<Zip<T, T>, F>;
pub trait FixedXORable<T, F> {
fn fixed_xor(self, other: T) -> FixedXOR<T, F>;
} }
pub fn fixed(a: &str, b: &str) -> Result<String, FixedXORError> { impl<'a, T, F> FixedXORable<T, F> for T
let a_decoded = a.hex_bytes().valid(); where T: Iterator<Item=u8> + 'a,
let b_decoded = b.hex_bytes().valid(); F: Fn((u8,u8)) -> u8
let xor: Vec<u8> = a_decoded.zip(b_decoded).map(|(x, y)| x ^ y).collect(); {
Ok(xor.hex_digest()) fn fixed_xor(self, other: T) -> FixedXOR<T, F> {
fn xor(tup: (u8, u8)) -> u8 { tup.0 ^ tup.1 }
self.zip(other).map(xor)
}
} }
#[cfg(test)] //pub fn fixed(a: &str, b: &str) -> Result<String, FixedXORError> {
mod tests { // let a_decoded = a.hex_bytes().valid();
use hex::AsHexBytes; // let b_decoded = b.hex_bytes().valid();
use std::char; // let xor: Vec<u8> = a_decoded.zip(b_decoded).map(|(x, y)| x ^ y).collect();
use super::fixed; // Ok(xor.hex_digest())
//}
#[test] //
fn cryptopals() { //#[cfg(test)]
let a = "1c0111001f010100061a024b53535009181c"; //mod tests {
let b = "686974207468652062756c6c277320657965"; // use hex::AsHexBytes;
let ex_output = "746865206b696420646f6e277420706c6179"; // use std::char;
let output = fixed(a, b); // use super::fixed;
assert_eq!(output.unwrap(), ex_output); //
} // #[test]
// fn cryptopals() {
static ENGLISH_LETTER_FREQ: &'static str = "EARIOTNSLCUDPMHGBFYWKVXZJQ"; // let a = "1c0111001f010100061a024b53535009181c";
// let b = "686974207468652062756c6c277320657965";
fn letter_freq_score(input: &str) -> f32 { // let ex_output = "746865206b696420646f6e277420706c6179";
let mut score: f32 = 0.0; // let output = fixed(a, b);
score // assert_eq!(output.unwrap(), ex_output);
} // }
//
#[test] // static ENGLISH_LETTER_FREQ: &'static str = "EARIOTNSLCUDPMHGBFYWKVXZJQ";
fn cryptopals13() { //
let input = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"; // fn letter_freq_score(input: &str) -> f32 {
for key in 32u32..127 { // let mut score: f32 = 0.0;
let possible_output = input.hex_bytes().valid() // score
.map(|c| char::from_u32(c as u32 ^ key)) // }
.take_while(|c| c.is_some()) //
.map(|c| c.unwrap()) // #[test]
.collect::<String>(); // fn cryptopals13() {
println!("{}: {}", key, possible_output); // let input = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736";
} // for key in 32u32..127 {
} // let possible_output = input.hex_bytes().valid()
} // .map(|c| char::from_u32(c as u32 ^ key))
// .take_while(|c| c.is_some())
// .map(|c| c.unwrap())
// .collect::<String>();
// println!("{}: {}", key, possible_output);
// }
// }
//}