cryptopals

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

commit c34984041c5a79bb1f85041dae20742db55f49e2
parent 4f28bbdad6216a99f1a9e5bc4850fecb614153e7
Author: Jared Tobin <jared@jtobin.ca>
Date:   Thu, 22 Dec 2016 22:18:53 +1300

Tweak a couple of bins.

Diffstat:
Mlib/charfreq/src/main.rs | 15++++++++++-----
Mlib/single_byte_xor/src/main.rs | 7+++++--
2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/lib/charfreq/src/main.rs b/lib/charfreq/src/main.rs @@ -2,7 +2,7 @@ extern crate rustc_serialize as serialize; use serialize::hex::FromHex; use std::collections::HashMap; -use std::io::{self, Read}; +use std::env; use std::string::String; use std::vec::Vec; @@ -18,12 +18,16 @@ fn tally(vec: Vec<u8>) -> HashMap<u8, u8> { } fn main() { - let mut buffer = String::new(); + let args: Vec<String> = env::args().collect(); - io::stdin().read_to_string(&mut buffer) - .expect("charfreq: bad input"); + if args.len() != 2 { + println!("USAGE: ./charfreq HEX"); + return () + } + + let supplied_string = &args[1]; - let decoded = match buffer.from_hex() { + let decoded = match supplied_string.from_hex() { Err(err) => panic!("charfreq: {}", err), Ok(val) => val, }; @@ -33,6 +37,7 @@ fn main() { let best: Vec<(u8, u8)> = results.into_iter().take(5).collect(); + println!("original: {}", &supplied_string); println!("byte (frequency)"); println!("----------------"); for (val, count) in best { println!("{} ({})", val, count); } diff --git a/lib/single_byte_xor/src/main.rs b/lib/single_byte_xor/src/main.rs @@ -15,7 +15,10 @@ fn main() { } let supplied_string = &args[1]; - let supplied_byte = supplied_string.clone().into_bytes()[0]; + let supplied_byte = match supplied_string.parse::<u8>() { + Err(err) => panic!("single_byte_xor: failed parse, {}", err), + Ok(val) => val, + }; // deal with stdin let mut buffer = String::new(); @@ -36,6 +39,6 @@ fn main() { }; println!("original: {}", &buffer); - println!("xored with: {} ({})", supplied_string, supplied_byte); + println!("xored with: {} ({})", supplied_string, supplied_byte as char); println!("decrypted: {}", decrypted); }