Remove duplicated code and make base64 error handling better

This commit is contained in:
2022-08-14 09:12:36 -04:00
parent fbf26efa44
commit 5158c16d56
8 changed files with 132 additions and 88 deletions

View File

@@ -1,8 +1,8 @@
#![allow(dead_code)]
use crate::bytes::Bytes;
use crate::bytes_base64::BytesBase64;
use crate::ecb;
use std::io::{BufRead, BufReader, Write};
use crate::{ecb, utils};
use std::io::Write;
use std::str;
pub fn challenge1() {
@@ -38,15 +38,7 @@ pub fn challenge3() {
}
pub fn challenge4() {
fn read(path: &str) -> Vec<Bytes> {
let file = std::fs::File::open(path).unwrap();
let br = BufReader::new(file);
br.lines()
.map(|line| Bytes::from_hex(&line.unwrap()))
.collect()
}
let bytes_vector = read("data/4.txt");
let bytes_vector = utils::read_hex_lines("data/4.txt");
let mut xored_bytes_vector: Vec<Bytes> = vec![];
for i in 32..=127 {
for bytes in bytes_vector.iter() {
@@ -73,11 +65,6 @@ pub fn challenge5() {
}
pub fn challenge6() {
fn read(path: &str) -> Bytes {
let s = std::fs::read_to_string(path).unwrap();
BytesBase64::from_base64(&s).to_bytes()
}
fn _write(path: &str, bytes_base64: &BytesBase64) {
let width = 60;
let bytes = bytes_base64.to_string();
@@ -96,7 +83,7 @@ pub fn challenge6() {
fn _test_roundtrip() {
// Test that conversion from to bytes and back works
let bytes = read("data/6.txt");
let bytes = utils::read_base64("data/6.txt");
let bytes_base64 = BytesBase64::from_bytes(bytes);
_write("data/6.txt", &bytes_base64);
}
@@ -139,7 +126,7 @@ pub fn challenge6() {
}
_test_roundtrip();
let bytes = read("data/6.txt");
let bytes = utils::read_base64("data/6.txt");
let key_size = guess_key_size(&bytes);
let bytes_tranposed = transpose(&bytes, key_size);
let key = Bytes(
@@ -155,10 +142,6 @@ pub fn challenge6() {
}
pub fn challenge7() {
fn read(path: &str) -> Bytes {
let s = std::fs::read_to_string(path).unwrap();
BytesBase64::from_base64(&s).to_bytes()
}
let text = Bytes::from_utf8("We meet at burger king!");
let key = Bytes::from_utf8("YELLOW SUBMARINE");
let ciphertext = ecb::encrypt(&key, &text);
@@ -166,21 +149,13 @@ pub fn challenge7() {
if text != roundtrip {
panic!("ECB roundtrip not working.");
}
let ciphertext = read("data/7.txt");
let ciphertext = utils::read_base64("data/7.txt");
let data = ecb::decrypt(&key, &ciphertext);
let partial_msg = data.to_utf8()[..20].to_string();
println!("[okay] Challenge 7: {}...", partial_msg);
}
pub fn challenge8() {
fn read(path: &str) -> Vec<Bytes> {
let file = std::fs::File::open(path).unwrap();
let br = BufReader::new(file);
br.lines()
.map(|line| Bytes::from_hex(&line.unwrap()))
.collect()
}
fn rate(Bytes(v): &Bytes, size: usize) -> u32 {
let mut rating = 0;
let chunks: Vec<&[u8]> = v.chunks(size).collect();
@@ -193,7 +168,7 @@ pub fn challenge8() {
}
let expected_index: usize = 132;
let bytes_vector = read("data/8.txt");
let bytes_vector = utils::read_hex_lines("data/8.txt");
let ratings: Vec<u32> = bytes_vector.iter().map(|b| rate(&b, 16)).collect();
let min_rating = ratings.iter().min().unwrap();
let index = ratings.iter().position(|e| e == min_rating).unwrap();
@@ -208,7 +183,7 @@ pub fn challenge8() {
);
// More elegant solution.
let bytes_vector = read("data/8.txt");
let bytes_vector = utils::read_hex_lines("data/8.txt");
let ix: Vec<usize> = bytes_vector
.iter()
.enumerate()