Make minor style improvements

This commit is contained in:
2023-06-13 21:35:56 -04:00
parent d81a8c3c30
commit f6c549e1d9
3 changed files with 12 additions and 12 deletions

View File

@@ -62,30 +62,30 @@ impl Md4Core {
} }
fn compress(state: &mut [u32; 4], input: &[u8; BLOCK_BYTES]) { fn compress(state: &mut [u32; 4], input: &[u8; BLOCK_BYTES]) {
fn f(x: u32, y: u32, z: u32) -> u32 { const fn f(x: u32, y: u32, z: u32) -> u32 {
(x & y) | (!x & z) (x & y) | (!x & z)
} }
fn g(x: u32, y: u32, z: u32) -> u32 { const fn g(x: u32, y: u32, z: u32) -> u32 {
(x & y) | (x & z) | (y & z) (x & y) | (x & z) | (y & z)
} }
fn h(x: u32, y: u32, z: u32) -> u32 { const fn h(x: u32, y: u32, z: u32) -> u32 {
x ^ y ^ z x ^ y ^ z
} }
fn op1(a: u32, b: u32, c: u32, d: u32, k: u32, s: u32) -> u32 { const fn op1(a: u32, b: u32, c: u32, d: u32, k: u32, s: u32) -> u32 {
a.wrapping_add(f(b, c, d)).wrapping_add(k).rotate_left(s) a.wrapping_add(f(b, c, d)).wrapping_add(k).rotate_left(s)
} }
fn op2(a: u32, b: u32, c: u32, d: u32, k: u32, s: u32) -> u32 { const fn op2(a: u32, b: u32, c: u32, d: u32, k: u32, s: u32) -> u32 {
a.wrapping_add(g(b, c, d)) a.wrapping_add(g(b, c, d))
.wrapping_add(k) .wrapping_add(k)
.wrapping_add(0x5A82_7999) .wrapping_add(0x5A82_7999)
.rotate_left(s) .rotate_left(s)
} }
fn op3(a: u32, b: u32, c: u32, d: u32, k: u32, s: u32) -> u32 { const fn op3(a: u32, b: u32, c: u32, d: u32, k: u32, s: u32) -> u32 {
a.wrapping_add(h(b, c, d)) a.wrapping_add(h(b, c, d))
.wrapping_add(k) .wrapping_add(k)
.wrapping_add(0x6ED9_EBA1) .wrapping_add(0x6ED9_EBA1)

View File

@@ -10,7 +10,7 @@ pub fn challenge1() {
let expected = String::from("SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"); let expected = String::from("SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t");
let result = BytesBase64::from_bytes(input); let result = BytesBase64::from_bytes(input);
if result.to_string() == expected { if result.to_string() == expected {
println!("[okay] Challenge 1: {}", result); println!("[okay] Challenge 1: {result}");
} else { } else {
println!("[fail] Challenge 1"); println!("[fail] Challenge 1");
} }
@@ -34,7 +34,7 @@ pub fn challenge3() {
h.sort_by(|a, b| a.ascii_score().partial_cmp(&b.ascii_score()).unwrap()); h.sort_by(|a, b| a.ascii_score().partial_cmp(&b.ascii_score()).unwrap());
// let h: Vec<Bytes> = h.into_iter().filter(|b| b.is_ascii()).collect(); // let h: Vec<Bytes> = h.into_iter().filter(|b| b.is_ascii()).collect();
let r = h.last().unwrap().to_utf8(); let r = h.last().unwrap().to_utf8();
println!("[okay] Challenge 3: {}", r); println!("[okay] Challenge 3: {r}");
} }
pub fn challenge4() { pub fn challenge4() {
@@ -133,7 +133,7 @@ pub fn challenge6() {
let msg = Bytes::xor_cycle(&bytes, &key).to_utf8(); let msg = Bytes::xor_cycle(&bytes, &key).to_utf8();
let partial_msg = msg[..20].to_string(); let partial_msg = msg[..20].to_string();
println!("[okay] Challenge 6: {}...", partial_msg); println!("[okay] Challenge 6: {partial_msg}...");
} }
pub fn challenge7() { pub fn challenge7() {
@@ -145,7 +145,7 @@ pub fn challenge7() {
let ciphertext = utils::read_base64("data/7.txt"); let ciphertext = utils::read_base64("data/7.txt");
let data = ecb::decrypt(&key, &ciphertext); let data = ecb::decrypt(&key, &ciphertext);
let partial_msg = data.to_utf8()[..20].to_string(); let partial_msg = data.to_utf8()[..20].to_string();
println!("[okay] Challenge 7: {}...", partial_msg); println!("[okay] Challenge 7: {partial_msg}...");
} }
pub fn challenge8() { pub fn challenge8() {

View File

@@ -375,7 +375,7 @@ pub fn challenge16() {
fn encrypt(input: &str, key: &Bytes, iv: &Bytes) -> Bytes { fn encrypt(input: &str, key: &Bytes, iv: &Bytes) -> Bytes {
let mut r = String::new(); let mut r = String::new();
for c in input.chars() { for c in input.chars() {
assert!(c != ';' && c != '=', "encrypt: invalid char {}", c); assert!(c != ';' && c != '=', "encrypt: invalid char {c}");
} }
r.push_str("comment1=cooking%20MCs;userdata="); r.push_str("comment1=cooking%20MCs;userdata=");
r.push_str(input); r.push_str(input);
@@ -402,5 +402,5 @@ pub fn challenge16() {
let dict = parser::parse_key_value(&cleartext_stripped.to_utf8()); let dict = parser::parse_key_value(&cleartext_stripped.to_utf8());
let admin_status = dict.get("admin").unwrap(); let admin_status = dict.get("admin").unwrap();
assert_eq!(admin_status, "true"); assert_eq!(admin_status, "true");
println!("[okay] Challenge 16: admin={}", admin_status); println!("[okay] Challenge 16: admin={admin_status}");
} }