diff --git a/src/main.rs b/src/main.rs index 040cc57..24e5d78 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,52 @@ mod sha; fn main() { - test_sha(); + test_sha224(); + test_sha256(); } -fn test_sha(){ - println!("Input: \"\""); - println!("Expected: 0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); - println!("Result Hash: 0x{}", sha::sha256("".to_string())); +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!(" Input: \"abc\""); + println!(" Expected: 0xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); + println!(" Result Hash: 0x{}", sha::sha256("abc".to_string())); - println!("\n##########################################\n"); - - 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())); -} \ No newline at end of file + 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!("_____________________________"); +} + +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!("_____________________________"); +} diff --git a/src/sha/mod.rs b/src/sha/mod.rs index 809c4fe..6435940 100644 --- a/src/sha/mod.rs +++ b/src/sha/mod.rs @@ -58,8 +58,42 @@ fn pad_message(message: String) -> Vec { } //////////// 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::>() + .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::>() + .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,23 +108,19 @@ 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 = pad_message(message); - // Process each chunk 64 Byte of message vector + // Process each chunk 64 Byte of message vector for chunk in msg_vec.chunks(64) { - // Initialize temp variables and the word vector + // Initialize temp variables and the word vector let mut w: Vec = Vec::with_capacity(64); let mut t1: u32; let mut t2: u32; - // Fill first 16 Byte of the word vector with the chunk values + // Fill first 16 Byte of the word vector with the chunk values for t in 0..16 { w.push(chunk[t]); } @@ -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::>() - .join(""); + return hash; }