From e9369f52505ffcd8c27b04f4682c76a7e2c32508 Mon Sep 17 00:00:00 2001 From: _N0x Date: Fri, 1 Nov 2024 15:16:28 +0100 Subject: [PATCH] Replaced String parameters with &[u8] --- src/main.rs | 12 ++++++------ src/sha/mod.rs | 18 +++++++++++------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2f2fa49..5a73d6e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,32 +8,32 @@ fn main() { fn test_sha() { assert_eq!( "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - sha::sha256("".to_string()), + sha::sha256("".to_string().as_bytes()), "Testing hash for \"\"" ); assert_eq!( "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", - sha::sha256("abc".to_string()), + sha::sha256("abc".to_string().as_bytes()), "Testing hash for \"abc\"" ); assert_eq!( "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592", - sha::sha256("The quick brown fox jumps over the lazy dog".to_string()), + sha::sha256("The quick brown fox jumps over the lazy dog".to_string().as_bytes()), "Testing hash for \"The quick brown fox jumps over the lazy dog\"" ); assert_eq!( "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", - sha::sha224("".to_string()), + sha::sha224("".to_string().as_bytes()), "Testing hash for \"\"" ); assert_eq!( "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7", - sha::sha224("abc".to_string()), + sha::sha224("abc".to_string().as_bytes()), "Testing hash for \"abc\"" ); assert_eq!( "730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525", - sha::sha224("The quick brown fox jumps over the lazy dog".to_string()), + sha::sha224("The quick brown fox jumps over the lazy dog".to_string().as_bytes()), "Testing hash for \"The quick brown fox jumps over the lazy dog\"" ); } diff --git a/src/sha/mod.rs b/src/sha/mod.rs index 66c0d42..e719538 100644 --- a/src/sha/mod.rs +++ b/src/sha/mod.rs @@ -29,13 +29,13 @@ fn ssig1(x: u32) -> u32 { return ret; } -fn pad_message(msg: String) -> Vec { +fn pad_message(msg: &[u8]) -> Vec { let l = (msg.len() as u64) << 3; let k = ((448u64.wrapping_sub((l + 8) % 512) % 512) >> 3) as usize; let mut result_u8 = Vec::with_capacity(msg.len() + 1 + k + std::mem::size_of::()); - result_u8.extend(msg.as_bytes()); + result_u8.extend(msg); result_u8.push(0x80); result_u8.resize(result_u8.len() + k, 0u8); result_u8.extend(&l.to_be_bytes()); @@ -54,7 +54,7 @@ fn pad_message(msg: String) -> Vec { } //////////// END HELPER FUNCTIONS ///////////// -pub fn sha224(message: String) -> String { +pub fn sha224(message: &[u8]) -> String { // Set initial hash values let init_hash_value: [u32; 8] = [ 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, @@ -72,7 +72,7 @@ pub fn sha224(message: String) -> String { .join(""); } -pub fn sha256(message: String) -> String { +pub fn sha256(message: &[u8]) -> String { // Set initial hash values let init_hash_value: [u32; 8] = [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, @@ -89,7 +89,7 @@ pub fn sha256(message: String) -> String { .join(""); } -fn sha_u32_calculate(init_hash_value: [u32; 8], message: String) -> [u32; 8] { +fn sha_u32_calculate(init_hash_value: [u32; 8], message: &[u8]) -> [u32; 8] { // Fill constants array with values const K: [u32; 64] = [ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, @@ -109,10 +109,12 @@ fn sha_u32_calculate(init_hash_value: [u32; 8], message: String) -> [u32; 8] { // Pad message as required let msg_vec: Vec = pad_message(message); + // Initial word vector + let mut w: Vec = Vec::with_capacity(64); + // Process each chunk 64 Byte of message vector for chunk in msg_vec.chunks(64) { - // Initialize temp variables and the word vector - let mut w: Vec = Vec::with_capacity(64); + // Initialize temp variables let mut t1: u32; let mut t2: u32; @@ -168,6 +170,8 @@ fn sha_u32_calculate(init_hash_value: [u32; 8], message: String) -> [u32; 8] { hash[5] = f.wrapping_add(hash[5]); hash[6] = g.wrapping_add(hash[6]); hash[7] = h.wrapping_add(hash[7]); + + w.clear(); } // End of chunk processing // After processing the entire message concatenate the result into the final variable