summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2015-04-04 02:10:29 -0400
committerJesse Luehrs <doy@tozt.net>2015-04-04 02:10:29 -0400
commit72e8d6fdd8644381ee82dbcbaab208a2547c52ce (patch)
tree4b000b18207da8db71d089c4a026919e1ad029fe /tests
parent7bcccede3eaa3d2ef01aa274e46f27e1673a1f21 (diff)
downloadmatasano-72e8d6fdd8644381ee82dbcbaab208a2547c52ce.tar.gz
matasano-72e8d6fdd8644381ee82dbcbaab208a2547c52ce.zip
problem 20
i think this is as far as possible, anyway
Diffstat (limited to 'tests')
-rw-r--r--tests/lib.rs36
1 files changed, 36 insertions, 0 deletions
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<Vec<u8>> {
.collect();
}
+fn read_as_lines (filename: &str) -> Vec<Vec<u8>> {
+ 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<u8> {
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<Vec<u8>>, len: usize) -> Vec<Vec<u8>> {
+ 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)
+ );
+}