From 72e8d6fdd8644381ee82dbcbaab208a2547c52ce Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 4 Apr 2015 02:10:29 -0400 Subject: problem 20 i think this is as far as possible, anyway --- tests/lib.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'tests') diff --git a/tests/lib.rs b/tests/lib.rs index 65380cf..ec214f4 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -2,6 +2,7 @@ extern crate matasano; extern crate rustc_serialize as serialize; extern crate rand; +use std::ascii::AsciiExt; use std::borrow::ToOwned; use std::collections::HashMap; use std::io::prelude::*; @@ -27,6 +28,14 @@ fn read_as_base64_lines (filename: &str) -> Vec> { .collect(); } +fn read_as_lines (filename: &str) -> Vec> { + let fh = File::open(filename).unwrap(); + return std::io::BufStream::new(fh) + .lines() + .map(|line| line.unwrap().as_bytes().to_vec()) + .collect(); +} + fn read_as_base64 (filename: &str) -> Vec { let fh = File::open(filename).unwrap(); return std::io::BufStream::new(fh) @@ -426,3 +435,30 @@ fn problem_18 () { // .collect(); // let plaintexts = matasano::crack_fixed_nonce_ctr_substitutions(); // } + +#[test] +fn problem_20 () { + fn normalize (line_list: Vec>, len: usize) -> Vec> { + line_list + .iter() + .map(|line| line.to_ascii_lowercase()) + .map(|line| line.iter().take(len).map(|x| *x).collect()) + .collect() + } + + let key = random_aes_128_key(); + let ciphertexts = read_as_base64_lines("data/20.txt") + .iter() + .map(|line| matasano::aes_128_ctr(&line[..], &key[..], 0)) + .collect(); + let expected = read_as_lines("data/20.out.txt"); + + let plaintexts = matasano::crack_fixed_nonce_ctr_statistically( + ciphertexts + ); + + assert_eq!( + normalize(plaintexts, 27), + normalize(expected, 27) + ); +} -- cgit v1.2.3-54-g00ecf