Restuctured sha module

This commit is contained in:
_N0x 2024-11-11 22:58:21 +01:00
parent c7d508e551
commit c0dfa7f1d6
3 changed files with 99 additions and 98 deletions

View File

@ -1,50 +1,10 @@
mod sha;
mod aes;
mod sha;
fn main() {
println!("Running SHA tests...");
test_sha();
sha::test();
println!("Running AES tests...");
aes::test();
}
fn test_sha() {
assert_eq!(
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
sha::sha256("".to_string().as_bytes()),
"Testing hash for \"\""
);
assert_eq!(
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
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()
.as_bytes()
),
"Testing hash for \"The quick brown fox jumps over the lazy dog\""
);
assert_eq!(
"d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
sha::sha224("".to_string().as_bytes()),
"Testing hash for \"\""
);
assert_eq!(
"23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7",
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()
.as_bytes()
),
"Testing hash for \"The quick brown fox jumps over the lazy dog\""
);
}

View File

@ -1,58 +1,5 @@
//////////// HELPER FUNCTIONS /////////////
fn ch(x: u32, y: u32, z: u32) -> u32 {
let ret: u32 = (x & y) ^ (!x & z);
return ret;
}
fn maj(x: u32, y: u32, z: u32) -> u32 {
let ret: u32 = (x & y) ^ (x & z) ^ (y & z);
return ret;
}
fn bsig0(x: u32) -> u32 {
let ret: u32 = x.rotate_right(2) ^ x.rotate_right(13) ^ x.rotate_right(22);
return ret;
}
fn bsig1(x: u32) -> u32 {
let ret: u32 = x.rotate_right(6) ^ x.rotate_right(11) ^ x.rotate_right(25);
return ret;
}
fn ssig0(x: u32) -> u32 {
let ret: u32 = x.rotate_right(7) ^ x.rotate_right(18) ^ (x >> 3);
return ret;
}
fn ssig1(x: u32) -> u32 {
let ret: u32 = x.rotate_right(17) ^ x.rotate_right(19) ^ (x >> 10);
return ret;
}
fn pad_message(msg: &[u8]) -> Vec<u32> {
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::<u64>());
result_u8.extend(msg);
result_u8.push(0x80);
result_u8.resize(result_u8.len() + k, 0u8);
result_u8.extend(&l.to_be_bytes());
let mut result_u32 = Vec::with_capacity(result_u8.len() / 4); // Allocate the right size for u32 vector
for chunk in result_u8.chunks(4) {
let value = (chunk[0] as u32) << 24
| (chunk[1] as u32) << 16
| (chunk[2] as u32) << 8
| (chunk[3] as u32);
result_u32.push(value);
}
return result_u32;
}
//////////// END HELPER FUNCTIONS /////////////
mod utils;
use utils::*;
pub fn sha224(message: &[u8]) -> String {
// Set initial hash values
@ -177,3 +124,44 @@ fn sha_u32_calculate(init_hash_value: [u32; 8], message: &[u8]) -> [u32; 8] {
// After processing the entire message concatenate the result into the final variable
return hash;
}
pub fn test() {
assert_eq!(
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
sha256("".to_string().as_bytes()),
"Testing hash for \"\""
);
assert_eq!(
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
sha256("abc".to_string().as_bytes()),
"Testing hash for \"abc\""
);
assert_eq!(
"d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592",
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",
sha224("".to_string().as_bytes()),
"Testing hash for \"\""
);
assert_eq!(
"23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7",
sha224("abc".to_string().as_bytes()),
"Testing hash for \"abc\""
);
assert_eq!(
"730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525",
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\""
);
}

53
src/sha/utils.rs Normal file
View File

@ -0,0 +1,53 @@
pub fn ch(x: u32, y: u32, z: u32) -> u32 {
let ret: u32 = (x & y) ^ (!x & z);
return ret;
}
pub fn maj(x: u32, y: u32, z: u32) -> u32 {
let ret: u32 = (x & y) ^ (x & z) ^ (y & z);
return ret;
}
pub fn bsig0(x: u32) -> u32 {
let ret: u32 = x.rotate_right(2) ^ x.rotate_right(13) ^ x.rotate_right(22);
return ret;
}
pub fn bsig1(x: u32) -> u32 {
let ret: u32 = x.rotate_right(6) ^ x.rotate_right(11) ^ x.rotate_right(25);
return ret;
}
pub fn ssig0(x: u32) -> u32 {
let ret: u32 = x.rotate_right(7) ^ x.rotate_right(18) ^ (x >> 3);
return ret;
}
pub fn ssig1(x: u32) -> u32 {
let ret: u32 = x.rotate_right(17) ^ x.rotate_right(19) ^ (x >> 10);
return ret;
}
pub fn pad_message(msg: &[u8]) -> Vec<u32> {
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::<u64>());
result_u8.extend(msg);
result_u8.push(0x80);
result_u8.resize(result_u8.len() + k, 0u8);
result_u8.extend(&l.to_be_bytes());
let mut result_u32 = Vec::with_capacity(result_u8.len() / 4); // Allocate the right size for u32 vector
for chunk in result_u8.chunks(4) {
let value = (chunk[0] as u32) << 24
| (chunk[1] as u32) << 16
| (chunk[2] as u32) << 8
| (chunk[3] as u32);
result_u32.push(value);
}
return result_u32;
}