Clean up before challenge 6.

This commit is contained in:
2022-03-25 21:38:05 -04:00
parent 495f305c11
commit cb28436fce
5 changed files with 534 additions and 154 deletions

View File

@@ -1,144 +1,10 @@
use std::fmt::Write;
use std::num::ParseIntError;
use crate::hexbytes::HexBytes;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::str;
#[derive(PartialEq, PartialOrd, Debug)]
pub struct HexBytes(Vec<u8>);
pub struct Base64Bytes(Vec<u8>);
impl std::fmt::Display for Base64Bytes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Base64({})", self.to_base64_string())
}
}
impl Base64Bytes {
pub fn to_base64_string(&self) -> String {
let Base64Bytes(digits) = self;
let mut r: Vec<u8> = digits
.iter()
.map(|d| match d {
0..=25 => *d + ('A' as u8),
26..=51 => *d - 26 + ('a' as u8),
52..=61 => *d - 52 + ('0' as u8),
62 => '+' as u8,
63 => '/' as u8,
_ => panic!("Unexpected base64 digit '{}'", d),
})
.collect();
// Handle padding
let pad = '=' as u8;
match r.len() % 4 {
0 => (),
2 => {
r.push(pad);
r.push(pad);
}
3 => r.push(pad),
_ => (),
}
str::from_utf8(r.as_slice()).unwrap().to_string()
}
}
impl HexBytes {
pub fn to_base64(&self) -> Base64Bytes {
fn chunk_to_base64(c: &[u8]) -> Vec<u8> {
let (value, iterations) = match c.len() {
0 => return vec![],
1 => ((c[0] as u32) << 16, 2),
2 => ((c[0] as u32) << 16 | (c[1] as u32) << 8, 3),
3 => ((c[0] as u32) << 16 | (c[1] as u32) << 8 | (c[2] as u32), 4),
_ => panic!("Unexpected number of chunks {}.", c.len()),
};
(0..iterations)
.map(|i| (value.rotate_right((3 - i) * 6) & 0b111111) as u8)
.collect()
}
let HexBytes(bytes) = self;
Base64Bytes(
bytes
.chunks(3)
.map(|c| chunk_to_base64(c))
.flatten()
.collect(),
)
}
pub fn from_str(s: &str) -> HexBytes {
if s.len() % 2 != 0 {
panic!("Input string has uneven number of characters");
}
let bytes_result: Result<Vec<u8>, ParseIntError> = (0..s.len())
.step_by(2)
.map(|i| u8::from_str_radix(&s[i..i + 2], 16))
.collect();
match bytes_result {
Ok(b) => HexBytes(b),
Err(_) => panic!("Could not convert all digit pairs to hex."),
}
}
#[allow(dead_code)]
pub fn to_utf8_string(&self) -> String {
let HexBytes(v) = self;
String::from(str::from_utf8(&v).unwrap())
}
pub fn to_hex_string(&self) -> String {
let HexBytes(v) = self;
let mut r = String::new();
for e in v.iter() {
write!(r, "{:02x}", e);
}
r
}
fn is_ascii(&self) -> bool {
let HexBytes(v) = self;
for &c in v.iter() {
if c < 32 || c > 127 {
return false;
}
}
true
}
fn ascii_score(&self) -> u32 {
let HexBytes(v) = self;
let mut r = 0;
for &c in v.iter() {
match c {
32 => r += 2,
33..=64 => r += 1,
65..=90 => r += 3,
91..=96 => r += 1,
97..=122 => r += 3,
123..=127 => r += 1,
_ => (),
}
}
r
}
fn xor(HexBytes(a): &HexBytes, HexBytes(b): &HexBytes) -> HexBytes {
HexBytes(
Iterator::zip(a.iter(), b.iter())
.map(|z| *(z.0) ^ *(z.1))
.collect(),
)
}
fn xor_byte(HexBytes(a): &HexBytes, byte: u8) -> HexBytes {
HexBytes(a.iter().map(|e| e ^ byte).collect())
}
}
pub fn challenge1() {
let a = HexBytes::from_str("49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d");
let a = HexBytes::from_hex_str("49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d");
let e = String::from("SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t");
let r = a.to_base64();
if r.to_base64_string() == e {
@@ -149,10 +15,10 @@ pub fn challenge1() {
}
pub fn challenge2() {
let a = HexBytes::from_str("1c0111001f010100061a024b53535009181c");
let b = HexBytes::from_str("686974207468652062756c6c277320657965");
let a = HexBytes::from_hex_str("1c0111001f010100061a024b53535009181c");
let b = HexBytes::from_hex_str("686974207468652062756c6c277320657965");
let r = HexBytes::xor(&a, &b);
let e = HexBytes::from_str("746865206b696420646f6e277420706c6179");
let e = HexBytes::from_hex_str("746865206b696420646f6e277420706c6179");
if r == e {
println!("[okay] Challenge 2: {}", r.to_hex_string());
} else {
@@ -161,18 +27,52 @@ pub fn challenge2() {
}
pub fn challenge3() {
let a =
HexBytes::from_str("1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736");
let a = HexBytes::from_hex_str(
"1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736",
);
let mut h: Vec<HexBytes> = (0..=255).map(|i| HexBytes::xor_byte(&a, i)).collect();
h.sort_by(|a, b| a.ascii_score().partial_cmp(&b.ascii_score()).unwrap());
let h: Vec<HexBytes> = h.into_iter().filter(|b| b.is_ascii()).collect();
let r = h[h.len() - 1].to_utf8_string();
let e = String::from("Cooking MC's like a pound of bacon");
println!("[okay] Challenge 3: {}", r);
}
if r == e {
println!("[okay] Challenge 3: {}", r);
pub fn challenge4() {
pub fn read_to_vector(path: &str) -> Vec<HexBytes> {
let file = File::open(path).unwrap();
let br = BufReader::new(file);
br.lines()
.map(|line| HexBytes::from_hex_str(&line.unwrap()))
.collect()
}
let bs = read_to_vector("data/4.txt");
let mut h: Vec<HexBytes> = vec![];
for i in 0..=255 {
for b in bs.iter() {
h.push(HexBytes::xor_byte(b, i));
}
}
h.sort_by(|a, b| a.ascii_score().partial_cmp(&b.ascii_score()).unwrap());
let r = h[h.len() - 1].to_utf8_string();
println!("[okay] Challenge 4: {}", r.trim());
}
pub fn challenge5() {
let msg = HexBytes::from_str(
"Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal",
);
let key = HexBytes::from_str("ICE");
let enc = HexBytes::xor_cycle(&msg, &key);
let exp = HexBytes::from_hex_str("0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f");
if enc == exp {
println!("[okay] Challenge 5: {}", enc.to_hex_string());
} else {
println!("[fail] Challenge 3: {}", r);
println!("[fail] Challenge 5")
}
}
pub fn challenge6() {
println!("[open] Challenge 6: xxx");
}