Added sha224 handling to sha module

This commit is contained in:
_N0x 2024-10-28 16:01:03 +01:00
parent 5033eacb00
commit 58a725074f
2 changed files with 84 additions and 28 deletions

View File

@ -1,23 +1,52 @@
mod sha;
fn main() {
test_sha();
test_sha224();
test_sha256();
}
fn test_sha(){
fn test_sha256() {
println!(" Input: \"\"");
println!(" Expected: 0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
println!(" Result Hash: 0x{}", sha::sha256("".to_string()));
println!("\n##########################################\n");
println!("_____________________________");
println!(" Input: \"abc\"");
println!(" Expected: 0xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
println!(" Result Hash: 0x{}", sha::sha256("abc".to_string()));
println!("\n##########################################\n");
println!("_____________________________");
println!(" Input: \"The quick brown fox jumps over the lazy dog\"");
println!(" Expected: 0xd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592");
println!("Result Hash: 0x{}", sha::sha256("The quick brown fox jumps over the lazy dog".to_string()));
println!(
" Result Hash: 0x{}",
sha::sha256("The quick brown fox jumps over the lazy dog".to_string())
);
println!("_____________________________");
}
fn test_sha224() {
println!(" Input: \"\"");
println!(" Expected: 0xd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f");
println!(" Result Hash: 0x{}", sha::sha224("".to_string()));
println!("_____________________________");
println!(" Input: \"abc\"");
println!(" Expected: 0x23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7");
println!(" Result Hash: 0x{}", sha::sha224("abc".to_string()));
println!("_____________________________");
println!(" Input: \"The quick brown fox jumps over the lazy dog\"");
println!(" Expected: 0x730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525");
println!(
" Result Hash: 0x{}",
sha::sha224("The quick brown fox jumps over the lazy dog".to_string())
);
println!("_____________________________");
}

View File

@ -58,8 +58,42 @@ fn pad_message(message: String) -> Vec<u32> {
}
//////////// END HELPER FUNCTIONS /////////////
pub fn sha256(message: String) -> String{
pub fn sha224(message: String) -> String {
// Set initial hash values
let init_hash_value: [u32; 8] = [
0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7,
0xbefa4fa4,
];
let hash = sha_u32_calculate(init_hash_value, message);
let hash = &hash[..7];
// After processing the entire message concatenate the result into the final variable
return hash
.iter()
.map(|&num| format!("{:08x}", num))
.collect::<Vec<String>>()
.join("");
}
pub fn sha256(message: String) -> String {
// Set initial hash values
let init_hash_value: [u32; 8] = [
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab,
0x5be0cd19,
];
let hash = sha_u32_calculate(init_hash_value, message);
// After processing the entire message concatenate the result into the final variable
return hash
.iter()
.map(|&num| format!("{:08x}", num))
.collect::<Vec<String>>()
.join("");
}
fn sha_u32_calculate(init_hash_value: [u32; 8], message: String) -> [u32; 8] {
// Fill constants array with values
const K: [u32; 64] = [
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4,
@ -74,11 +108,7 @@ pub fn sha256(message: String) -> String{
0xc67178f2,
];
// Set initial hash values
let mut hash: [u32; 8] = [
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab,
0x5be0cd19,
];
let mut hash = init_hash_value;
// Pad message as required
let msg_vec: Vec<u32> = pad_message(message);
@ -145,8 +175,5 @@ pub fn sha256(message: String) -> String{
} // End of chunk processing
// After processing the entire message concatenate the result into the final variable
return hash.iter()
.map(|&num| format!("{:08x}", num))
.collect::<Vec<String>>()
.join("");
return hash;
}