Refactor some more, done for now

This commit is contained in:
2022-08-25 18:33:46 -04:00
parent b97b2fe6d0
commit 0951a6ab3e
5 changed files with 28 additions and 36 deletions

View File

@@ -17,13 +17,9 @@ pub fn parse_key_value(text: &str) -> HashMap<String, String> {
tokens = scan(text, 0, tokens);
for token_chunk in tokens.chunks(4) {
match token_chunk {
[Token::Identifier(key), Token::Equal, Token::Identifier(value), Token::Ampersand] => {
result.insert(key.to_string(), value.to_string());
}
[Token::Identifier(key), Token::Equal, Token::Identifier(value), Token::Semicolon] => {
result.insert(key.to_string(), value.to_string());
}
[Token::Identifier(key), Token::Equal, Token::Identifier(value)] => {
[Token::Identifier(key), Token::Equal, Token::Identifier(value), Token::Ampersand]
| [Token::Identifier(key), Token::Equal, Token::Identifier(value), Token::Semicolon]
| [Token::Identifier(key), Token::Equal, Token::Identifier(value)] => {
result.insert(key.to_string(), value.to_string());
}
_ => panic!("Could not parse {:?}", token_chunk),

View File

@@ -12,7 +12,7 @@ pub fn challenge1() {
if result.to_string() == expected {
println!("[okay] Challenge 1: {}", result);
} else {
println!("[fail] Challenge 1")
println!("[fail] Challenge 1");
}
}
@@ -24,7 +24,7 @@ pub fn challenge2() {
if result == expected {
println!("[okay] Challenge 2: {}", result.to_hex());
} else {
println!("[fail] Challenge 2")
println!("[fail] Challenge 2");
}
}
@@ -41,7 +41,7 @@ pub fn challenge4() {
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() {
for bytes in &bytes_vector {
xored_bytes_vector.push(Bytes::xor_byte(bytes, i));
}
}
@@ -141,9 +141,7 @@ pub fn challenge7() {
let key = Bytes::from_utf8("YELLOW SUBMARINE");
let ciphertext = ecb::encrypt(&key, &text);
let roundtrip = ecb::decrypt(&key, &ciphertext);
if text != roundtrip {
panic!("ECB roundtrip not working.");
}
assert!(text == roundtrip, "ECB roundtrip not working.");
let ciphertext = utils::read_base64("data/7.txt");
let data = ecb::decrypt(&key, &ciphertext);
let partial_msg = data.to_utf8()[..20].to_string();

View File

@@ -144,10 +144,10 @@ pub fn challenge19() {
letters
}
fn attack(ciphers: Vec<Bytes>) -> Vec<RefCell<Vec<u8>>> {
fn attack(ciphers: &[Bytes]) -> Vec<RefCell<Vec<u8>>> {
let ciphers_len = ciphers.len();
let deciphered = vec![RefCell::new(vec![]); ciphers_len];
let max_cipher_len = ciphers.iter().map(|c| c.len()).max().unwrap_or(0);
let max_cipher_len = ciphers.iter().map(Bytes::len).max().unwrap_or(0);
for byte_index in 0..max_cipher_len {
let letters = match byte_index {
@@ -170,7 +170,7 @@ pub fn challenge19() {
.collect();
let mut possible_chars: Vec<HashSet<u8>> = ciphers
.iter()
.map(|_| HashSet::from_iter(letters.iter().cloned()))
.map(|_| letters.iter().cloned().collect())
.collect();
for i in 0..ciphers_len {
@@ -180,8 +180,8 @@ pub fn challenge19() {
}
let xored = target_bytes[i].unwrap() ^ target_bytes[j].unwrap();
let chars = lookup.get(&xored).unwrap().borrow();
possible_chars[i] = possible_chars[i].intersection(&chars).cloned().collect();
possible_chars[j] = possible_chars[j].intersection(&chars).cloned().collect();
possible_chars[i] = possible_chars[i].intersection(&chars).copied().collect();
possible_chars[j] = possible_chars[j].intersection(&chars).copied().collect();
}
}
@@ -244,7 +244,7 @@ pub fn challenge19() {
let key = Bytes::from_utf8("YELLOW SUBMARINE");
let encrypt = |plaintext: &Bytes| -> Bytes { ctr::encrypt(&key, 0, plaintext) };
let ciphers: Vec<Bytes> = plaintexts.iter().map(encrypt).collect();
let decrypts = attack(ciphers);
let decrypts = attack(&ciphers);
manual(&decrypts);
let first_line = Bytes(decrypts[0].borrow().to_vec()).to_utf8();
println!("[okay] Challenge 19: {first_line}");
@@ -322,7 +322,7 @@ pub fn challenge22() {
}
pub fn challenge23() {
fn _temper(x: u32) -> u32 {
const fn _temper(x: u32) -> u32 {
const S: u32 = 7;
const T: u32 = 15;
const U: u32 = 11;

View File

@@ -13,10 +13,10 @@ pub fn challenge25() {
// function, like, "edit(ciphertext, key, offset, newtext)".
fn edit(ciphertext: &Bytes, key: &Bytes, offset: usize, newtext: &Vec<u8>) -> Bytes {
let mut plaintext = ctr::decrypt(key, 0, ciphertext);
if offset + newtext.len() > plaintext.len() {
panic!("challenge25 - edit - out of bounds");
}
assert!(
offset + newtext.len() <= plaintext.len(),
"challenge25 - edit - out of bounds"
);
plaintext.0[offset..(newtext.len() + offset)].copy_from_slice(&newtext[..]);
ctr::encrypt(key, 0, &plaintext)
}
@@ -44,9 +44,7 @@ pub fn challenge26() {
fn encrypt(input: &str, key: &Bytes) -> Bytes {
let mut r = String::new();
for c in input.chars() {
if c == ';' || c == '=' {
panic!("encrypt: invalid char {}", c);
}
assert!(!(c == ';' || c == '='), "encrypt: invalid char {}", c);
}
r.push_str("comment1=cooking%20MCs;userdata=");
r.push_str(input);
@@ -101,10 +99,10 @@ pub fn challenge27() {
fn modify(cipher: &Bytes) -> Bytes {
// C_1, C_2, C_3 -> C_1, 0, C_1
let c1 = cipher.get_block(0, 16).0;
let mut modified = c1.to_vec();
let mut c1 = cipher.get_block(0, 16).0;
let mut modified = c1.clone();
modified.append(&mut vec![0; 16]);
modified.append(&mut c1.to_vec());
modified.append(&mut c1);
Bytes(modified)
}
@@ -217,8 +215,8 @@ pub fn challenge29() {
// forge message: original-message || glue-padding || new-message
forged_message = message.0.to_vec();
forged_message.append(&mut glue_padding.to_vec());
forged_message.append(&mut new_message.to_vec());
forged_message.append(&mut glue_padding.clone());
forged_message.append(&mut new_message.clone());
let r = sha1::verify(&Bytes(forged_message.to_vec()), &key, &mac_forged);
if r {
break;
@@ -290,7 +288,7 @@ pub fn challenge30() {
forged_message = message.0.to_vec();
forged_message.append(&mut glue_padding.to_vec());
forged_message.append(&mut new_message.to_vec());
let r = md4::verify(&Bytes(forged_message.to_vec()), &key, &mac_forged);
let r = md4::verify(&Bytes(forged_message.clone()), &key, &mac_forged);
if r {
break;
}

View File

@@ -1,4 +1,4 @@
/// SHA-1 implementation adapted from https://github.com/vog/sha1/blob/master/sha1.hpp
/// SHA-1 implementation adapted from <https://github.com/vog/sha1/blob/master/sha1.hpp>
use crate::bytes::Bytes;
const STATE_LEN: usize = 5;
@@ -255,8 +255,8 @@ pub fn authenticate(message: &Bytes, key: &Bytes) -> Bytes {
// secret-prefix MAC, which is simply:
// SHA1(key || message)
let mut c = vec![];
c.append(&mut key.0.to_vec());
c.append(&mut message.0.to_vec());
c.append(&mut key.0.clone());
c.append(&mut message.0.clone());
// how to concatenate better: https://stackoverflow.com/a/56490417
let mut sha1 = Sha1::default();
sha1.hash(&Bytes(c))