cryptopals

Matasano's cryptopals challenges (cryptopals.com).
Log | Files | Refs | README | LICENSE

commit 6538d1a3903444363c02fc819d0105083eca9d98
parent 6494621f70376ecb3feb2a234306dca3793709a4
Author: Jared Tobin <jared@jtobin.ca>
Date:   Sun, 27 Aug 2017 17:36:21 +1200

Add structured Rust code.

Diffstat:
M.gitignore | 3+++
ACargo.toml | 9+++++++++
Asrc/main.rs | 10++++++++++
Asrc/s1c1.rs | 23+++++++++++++++++++++++
Asrc/s1c2.rs | 22++++++++++++++++++++++
Asrc/s1c3.rs | 45+++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 112 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -7,3 +7,6 @@ target *.hi etc/working deprecated +/target/ +**/*.rs.bk +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "cryptopals" +version = "0.1.0" +authors = ["Jared Tobin <jared@jtobin.ca>"] + +[dependencies] +base64 = "0.6.0" +hex = "0.2.0" + diff --git a/src/main.rs b/src/main.rs @@ -0,0 +1,10 @@ + +mod s1c1; +mod s1c2; +mod s1c3; + +fn main() { + println!("s1c1:\n{}\n", s1c1::s1c1()); + println!("s1c2:\n{}\n", s1c2::s1c2()); + println!("s1c3:\n{}\n", s1c3::s1c3()); +} diff --git a/src/s1c1.rs b/src/s1c1.rs @@ -0,0 +1,23 @@ + +extern crate hex; +extern crate base64; + +use self::hex::{FromHex, FromHexError}; +use std::process; + +const INPUT: &str = + "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6\ + f7573206d757368726f6f6d"; + +pub fn hex_to_b64(input: &str) -> Result<String, FromHexError> { + let raw: Result<Vec<u8>, _> = FromHex::from_hex(&input); + raw.map(|contents| base64::encode(&contents)) +} + +pub fn s1c1() -> String { + hex_to_b64(&INPUT).unwrap_or_else(|err| { + println!("error (cryptopals): {}", err); + process::exit(1); + }) +} + diff --git a/src/s1c2.rs b/src/s1c2.rs @@ -0,0 +1,22 @@ + +extern crate hex; + +use self::hex::{FromHex, ToHex}; + +const TARGET: &str = "1c0111001f010100061a024b53535009181c"; +const PARTNER: &str = "686974207468652062756c6c277320657965"; + +fn fixed_xor(target: &str, partner: &str) -> String { + assert_eq!(target.len(), partner.len()); + + let mut l: Vec<u8> = FromHex::from_hex(&target).unwrap(); + let r: Vec<u8> = FromHex::from_hex(&partner).unwrap(); + + for (lb, rb) in l.iter_mut().zip(r) { *lb ^= rb } + + l.to_hex() +} + +pub fn s1c2() -> String { + fixed_xor(&TARGET, &PARTNER) +} diff --git a/src/s1c3.rs b/src/s1c3.rs @@ -0,0 +1,45 @@ + +extern crate hex; + +use self::hex::FromHex; +use std::collections::HashMap; + +const HASH: &'static str = + "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"; + +fn tally(vec: Vec<u8>) -> HashMap<u8, u8> { + let mut hashmap = HashMap::new(); + + for byte in vec { + let count = hashmap.entry(byte).or_insert(0); + *count += 1; + } + + hashmap +} + +fn max_elem(hashmap: HashMap<u8, u8>) -> u8 { + let mut max = 0; + let mut max_index = 0; + + for (byte, count) in hashmap.iter() { + if count > &max { + max = *count; + max_index = *byte; + } + } + + max_index +} + +pub fn s1c3() -> String { + let mut bytes: Vec<u8> = FromHex::from_hex(&HASH).unwrap(); + + let tallied = tally(bytes.clone()); + let max = max_elem(tallied); + + for byte in bytes.iter_mut() { *byte ^= max - 32; } + + String::from_utf8(bytes).unwrap() +} +