Replaced String parameters with &[u8]

This commit is contained in:
_N0x 2024-11-01 15:16:28 +01:00
parent a01fd584fc
commit e9369f5250
2 changed files with 17 additions and 13 deletions

View File

@ -8,32 +8,32 @@ fn main() {
fn test_sha() { fn test_sha() {
assert_eq!( assert_eq!(
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
sha::sha256("".to_string()), sha::sha256("".to_string().as_bytes()),
"Testing hash for \"\"" "Testing hash for \"\""
); );
assert_eq!( assert_eq!(
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
sha::sha256("abc".to_string()), sha::sha256("abc".to_string().as_bytes()),
"Testing hash for \"abc\"" "Testing hash for \"abc\""
); );
assert_eq!( assert_eq!(
"d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592", "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\"" "Testing hash for \"The quick brown fox jumps over the lazy dog\""
); );
assert_eq!( assert_eq!(
"d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
sha::sha224("".to_string()), sha::sha224("".to_string().as_bytes()),
"Testing hash for \"\"" "Testing hash for \"\""
); );
assert_eq!( assert_eq!(
"23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7", "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7",
sha::sha224("abc".to_string()), sha::sha224("abc".to_string().as_bytes()),
"Testing hash for \"abc\"" "Testing hash for \"abc\""
); );
assert_eq!( assert_eq!(
"730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525", "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\"" "Testing hash for \"The quick brown fox jumps over the lazy dog\""
); );
} }

View File

@ -29,13 +29,13 @@ fn ssig1(x: u32) -> u32 {
return ret; return ret;
} }
fn pad_message(msg: String) -> Vec<u32> { fn pad_message(msg: &[u8]) -> Vec<u32> {
let l = (msg.len() as u64) << 3; let l = (msg.len() as u64) << 3;
let k = ((448u64.wrapping_sub((l + 8) % 512) % 512) >> 3) as usize; 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::<u64>()); let mut result_u8 = Vec::with_capacity(msg.len() + 1 + k + std::mem::size_of::<u64>());
result_u8.extend(msg.as_bytes()); result_u8.extend(msg);
result_u8.push(0x80); result_u8.push(0x80);
result_u8.resize(result_u8.len() + k, 0u8); result_u8.resize(result_u8.len() + k, 0u8);
result_u8.extend(&l.to_be_bytes()); result_u8.extend(&l.to_be_bytes());
@ -54,7 +54,7 @@ fn pad_message(msg: String) -> Vec<u32> {
} }
//////////// END HELPER FUNCTIONS ///////////// //////////// END HELPER FUNCTIONS /////////////
pub fn sha224(message: String) -> String { pub fn sha224(message: &[u8]) -> String {
// Set initial hash values // Set initial hash values
let init_hash_value: [u32; 8] = [ let init_hash_value: [u32; 8] = [
0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7,
@ -72,7 +72,7 @@ pub fn sha224(message: String) -> String {
.join(""); .join("");
} }
pub fn sha256(message: String) -> String { pub fn sha256(message: &[u8]) -> String {
// Set initial hash values // Set initial hash values
let init_hash_value: [u32; 8] = [ let init_hash_value: [u32; 8] = [
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab,
@ -89,7 +89,7 @@ pub fn sha256(message: String) -> String {
.join(""); .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 // Fill constants array with values
const K: [u32; 64] = [ const K: [u32; 64] = [
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 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 // Pad message as required
let msg_vec: Vec<u32> = pad_message(message); let msg_vec: Vec<u32> = pad_message(message);
// Initial word vector
let mut w: Vec<u32> = Vec::with_capacity(64);
// Process each chunk 64 Byte of message vector // Process each chunk 64 Byte of message vector
for chunk in msg_vec.chunks(64) { for chunk in msg_vec.chunks(64) {
// Initialize temp variables and the word vector // Initialize temp variables
let mut w: Vec<u32> = Vec::with_capacity(64);
let mut t1: u32; let mut t1: u32;
let mut t2: 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[5] = f.wrapping_add(hash[5]);
hash[6] = g.wrapping_add(hash[6]); hash[6] = g.wrapping_add(hash[6]);
hash[7] = h.wrapping_add(hash[7]); hash[7] = h.wrapping_add(hash[7]);
w.clear();
} // End of chunk processing } // End of chunk processing
// After processing the entire message concatenate the result into the final variable // After processing the entire message concatenate the result into the final variable