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

@@ -17,9 +17,7 @@ pub fn challenge25() {
panic!("challenge25 - edit - out of bounds");
}
for i in 0..newtext.len() {
plaintext.0[offset + i] = newtext[i];
}
plaintext.0[offset..(newtext.len() + offset)].copy_from_slice(&newtext[..]);
ctr::encrypt(key, 0, &plaintext)
}
@@ -64,12 +62,12 @@ pub fn challenge26() {
// comment1=cooking%20MCs;userdata=aaaaaaaaaaaaaaaa;comment2=%20like%20a%20pound%20of%20bacon
// comment1=cooking%20MCs;userdata=fobar;admin=true;comment2=%20like%20a%20pound%20of%20bacon
let input = "aaaaaaaaaaaaaaaa";
let cipher = encrypt(&input, &key);
let cipher = encrypt(input, &key);
let keystream = utils::xor(&cipher.0[32..48], &Bytes::from_utf8(input).0);
let input = "fobar;admin=true";
let mut flipped_cipher = cipher.0[0..32].to_vec();
flipped_cipher.append(&mut utils::xor(&keystream, &input.as_bytes()));
flipped_cipher.append(&mut utils::xor(&keystream, input.as_bytes()));
flipped_cipher.append(&mut cipher.0[48..cipher.len()].to_vec());
let cleartext = ctr::decrypt(&key, 0, &Bytes(flipped_cipher));
@@ -84,7 +82,7 @@ pub fn challenge27() {
// AES-CBC(P_1, P_2, P_3) -> C_1, C_2, C_3
let mut ct = Bytes::from_utf8("comment1=cooking%20MCs;userdata=secretsaucenouse");
ct.pad_pkcs7(16);
cbc::encrypt(&key, &key, &ct)
cbc::encrypt(key, key, &ct)
}
fn decrypt(key: &Bytes, cipher: &Bytes) -> Result<Bytes, Bytes> {
@@ -93,7 +91,7 @@ pub fn challenge27() {
// values). Noncompliant messages should raise an exception or return an
// error that includes the decrypted plaintext (this happens all the
// time in real systems, for what it's worth).
let mut cleartext = cbc::decrypt(&key, &key, &cipher);
let mut cleartext = cbc::decrypt(key, key, cipher);
cleartext.remove_pkcs7(16);
match std::str::from_utf8(&cleartext.0) {
Ok(_) => Ok(cleartext),
@@ -191,7 +189,7 @@ pub fn challenge29() {
// With the registers "fixated", hash the additional data you want to
// forge.
s.fix(fixate.try_into().unwrap(), byte_len);
s.hash(&bytes)
s.hash(bytes)
}
// use random
@@ -263,7 +261,7 @@ pub fn challenge30() {
.map(|c| u32::from_le_bytes(c.try_into().unwrap()))
.collect();
m.fix(fixate.try_into().unwrap(), byte_len);
m.hash(&bytes)
m.hash(bytes)
}
let key = Bytes::random_range(2, 64);