From 1f7c4e6571201d05f5c67e5c709d15ae4a40e059 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Tue, 7 Oct 2025 22:40:31 +0800 Subject: [PATCH] chore: use "is_multiple_of" method --- common/src/lib.rs | 89 +++++++++++++++++++++++++++++++++++++--- problems/p21/src/main.rs | 2 +- problems/p28/src/main.rs | 1 - 3 files changed, 85 insertions(+), 7 deletions(-) diff --git a/common/src/lib.rs b/common/src/lib.rs index 84f45f4..daf60d2 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -267,7 +267,7 @@ fn g_func(word: [u8; 4], round: usize) -> [u8; 4] { } pub fn aes_ecb_enc(input: &[u8], key: &[u8; 16]) -> Result> { - if input.len() % 16 != 0 { + if !input.len().is_multiple_of(16) { return Err(anyhow!("Invalid input length")); } let mut cipher: Vec = Vec::new(); @@ -291,7 +291,7 @@ pub fn aes_ecb_enc(input: &[u8], key: &[u8; 16]) -> Result> { } pub fn aes_ecb_dec(input: &[u8], key: &[u8; 16]) -> Result> { - if input.len() % 16 != 0 { + if !input.len().is_multiple_of(16) { return Err(anyhow!("Invalid input length")); } @@ -317,7 +317,7 @@ pub fn aes_ecb_dec(input: &[u8], key: &[u8; 16]) -> Result> { } pub fn aes_cbc_enc(input: &[u8], key: &[u8; 16], iv: &[u8; 16]) -> Result> { - if input.len() % 16 != 0 { + if !input.len().is_multiple_of(16) { return Err(anyhow!("Invalid input length")); } let mut cipher: Vec = Vec::new(); @@ -350,7 +350,7 @@ pub fn aes_cbc_enc(input: &[u8], key: &[u8; 16], iv: &[u8; 16]) -> Result Result> { - if input.len() % 16 != 0 { + if !input.len().is_multiple_of(16) { return Err(anyhow!("Invalid input length")); } @@ -540,9 +540,88 @@ impl MT19937 { for i in 0..624 { let y: u32 = (self.mt[i] & 0x80000000) + (self.mt[(i + 1) % 624] & 0x7fffffff); self.mt[i] = self.mt[(i + 397) % 624] ^ (y >> 1); - if y % 2 != 0 { + if !y.is_multiple_of(2) { self.mt[i] ^= 0x9908b0df; } } } } + +pub fn sha1(input: &[u8]) -> [u8; 20] { + let mut h0 = 0x67452301u32; + let mut h1 = 0xEFCDAB89u32; + let mut h2 = 0x98BADCFEu32; + let mut h3 = 0x10325476u32; + let mut h4 = 0xC3D2E1F0u32; + + let message_bits_len = input.len() * 8; + let mut buffer = input.to_vec(); + buffer.push(0x80u8); + + while (buffer.len() % 64) != 56 { + buffer.push(0x00u8); + } + + buffer.extend_from_slice(&(message_bits_len as u64).to_be_bytes()); + + for chunk in buffer.chunks(64) { + let mut extend_buf = [0u32; 80]; + for i in 0..16 { + let bytes: [u8; 4] = chunk[4 * i..4 * i + 4].try_into().unwrap(); + extend_buf[i] = u32::from_be_bytes(bytes); + } + for i in 16..80 { + extend_buf[i] = + (extend_buf[i - 3] ^ extend_buf[i - 8] ^ extend_buf[i - 14] ^ extend_buf[i - 16]) + .rotate_left(1); + } + let mut a = h0; + let mut b = h1; + let mut c = h2; + let mut d = h3; + let mut e = h4; + for (i, &w_i) in extend_buf.iter().enumerate() { + let f = match i { + 0..20 => (b & c) | (!b & d), + 20..40 => b ^ c ^ d, + 40..60 => (b & c) | (b & d) | (c & d), + 60..80 => b ^ c ^ d, + _ => panic!("Invalid round: {}", i), + }; + let k = match i { + 0..20 => 0x5A827999, + 20..40 => 0x6ED9EBA1, + 40..60 => 0x8F1BBCDC, + 60..80 => 0xCA62C1D6, + _ => panic!("Invalid round: {}", i), + }; + let temp = a + .rotate_left(5) + .wrapping_add(f) + .wrapping_add(e) + .wrapping_add(w_i) + .wrapping_add(k); + + e = d; + d = c; + c = b.rotate_left(30); + b = a; + a = temp; + } + h0 = h0.wrapping_add(a); + h1 = h1.wrapping_add(b); + h2 = h2.wrapping_add(c); + h3 = h3.wrapping_add(d); + h4 = h4.wrapping_add(e); + } + + let mut digest = [0u8; 20]; + + digest[0..4].copy_from_slice(&h0.to_be_bytes()); + digest[4..8].copy_from_slice(&h1.to_be_bytes()); + digest[8..12].copy_from_slice(&h2.to_be_bytes()); + digest[12..16].copy_from_slice(&h3.to_be_bytes()); + digest[16..20].copy_from_slice(&h4.to_be_bytes()); + + digest +} diff --git a/problems/p21/src/main.rs b/problems/p21/src/main.rs index 6ce3a96..df7c685 100644 --- a/problems/p21/src/main.rs +++ b/problems/p21/src/main.rs @@ -77,7 +77,7 @@ impl MT19937 { for i in 0..624 { let y: u32 = (self.mt[i] & 0x80000000) + (self.mt[(i + 1) % 624] & 0x7fffffff); self.mt[i] = self.mt[(i + 397) % 624] ^ (y >> 1); - if y % 2 != 0 { + if !y.is_multiple_of(2) { self.mt[i] ^= 0x9908b0df; } } diff --git a/problems/p28/src/main.rs b/problems/p28/src/main.rs index 67abde3..2035fe8 100644 --- a/problems/p28/src/main.rs +++ b/problems/p28/src/main.rs @@ -100,7 +100,6 @@ // w[i] = (w[i-6] xor w[i-16] xor w[i-28] xor w[i-32]) leftrotate 2 // This transformation keeps all operands 64-bit aligned and, by removing the dependency of w[i] on w[i-3], allows efficient SIMD implementation with a vector length of 4 like x86 SSE instructions. -use hex; use sha1::{Digest, Sha1}; fn sha1(input: &[u8]) -> [u8; 20] {