Refactor code based on clippy suggestions

This commit is contained in:
2022-08-25 17:45:16 -04:00
parent 9a9b5335f1
commit b97b2fe6d0
16 changed files with 145 additions and 204 deletions

View File

@@ -46,8 +46,9 @@ pub fn challenge17() {
for pad_byte in 1..=block_size {
// preset attack vector so that paddinig is [1], [2, 2], [3, 3, 3], and so on.
attack_vector.0[block_size - pad_byte] = pad_byte as u8;
for i in 0..(pad_byte - 1) {
attack_vector.0[block_size - 1 - i] = (pad_byte as u8) ^ intermittent_result[i];
for (i, intermittent_byte) in intermittent_result.iter().enumerate().take(pad_byte - 1)
{
attack_vector.0[block_size - 1 - i] = (pad_byte as u8) ^ intermittent_byte;
}
// guess attack vector so that padding is valid
@@ -129,7 +130,7 @@ pub fn challenge19() {
}
fn u8_lower(s: u8) -> u8 {
if s >= b'A' && s <= b'Z' {
if (b'A'..=b'Z').contains(&s) {
return s + 32;
}
s
@@ -220,7 +221,7 @@ pub fn challenge19() {
deciphered
}
fn manual(decrypts: &Vec<RefCell<Vec<u8>>>) {
fn manual(decrypts: &[RefCell<Vec<u8>>]) {
// Add manually guessed letters
decrypts[0].borrow_mut()[30] = b'y';
decrypts[2].borrow_mut()[30] = b'y';
@@ -242,16 +243,11 @@ pub fn challenge19() {
let plaintexts = utils::read_base64_lines("data/19.txt");
let key = Bytes::from_utf8("YELLOW SUBMARINE");
let encrypt = |plaintext: &Bytes| -> Bytes { ctr::encrypt(&key, 0, plaintext) };
let ciphers: Vec<Bytes> = plaintexts.iter().map(|ct| encrypt(&ct)).collect();
let ciphers: Vec<Bytes> = plaintexts.iter().map(encrypt).collect();
let decrypts = attack(ciphers);
manual(&decrypts);
for row in decrypts {
println!(
"[okay] Challenge 19: {}",
Bytes(row.borrow().to_vec()).to_utf8()
);
break;
}
let first_line = Bytes(decrypts[0].borrow().to_vec()).to_utf8();
println!("[okay] Challenge 19: {first_line}");
}
pub fn challenge20() {
@@ -273,7 +269,7 @@ pub fn challenge20() {
let plaintexts = utils::read_base64_lines("data/20.txt");
let key = Bytes::from_utf8("YELLOW SUBMARINE");
let encrypt = |plaintext: &Bytes| -> Bytes { ctr::encrypt(&key, 0, plaintext) };
let ciphers: Vec<Bytes> = plaintexts.iter().map(|ct| encrypt(&ct)).collect();
let ciphers: Vec<Bytes> = plaintexts.iter().map(encrypt).collect();
let plaintexts = attack(ciphers);
println!("[okay] Challenge 20: {}", plaintexts[0].to_utf8());
@@ -287,8 +283,8 @@ pub fn challenge21() {
];
let mut mt = mt19937::MT19937::new();
mt.seed(5489);
for i in 0..expected.len() {
assert_eq!(mt.extract_number(), expected[i]);
for e in expected {
assert_eq!(mt.extract_number(), e);
}
println!("[okay] Challenge 21: implemented MT19937");
}
@@ -442,7 +438,7 @@ pub fn challenge24() {
// brute force bb!
for key in 0..u16::MAX {
let mut found_key = true;
let roundtrip = mtcipher::decrypt(key, &cipher);
let roundtrip = mtcipher::decrypt(key, cipher);
// check if the last 14 chars are 'A' - if yes, we found the key
for i in (cipher_len - 14)..cipher_len {
if roundtrip.0[i] != b'A' {
@@ -500,9 +496,9 @@ pub fn challenge24() {
false
}
assert_eq!(is_time_token(&token), true);
assert!(is_time_token(&token));
let non_token = Bytes(vec![b'z', 16]);
assert_eq!(is_time_token(&non_token), false);
assert!(!is_time_token(&non_token));
println!("[okay] Challenge 24: MT19937 stream cipher implemented and cracked");
}