//////////// 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 { 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); 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 ///////////// pub fn sha224(message: &[u8]) -> 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: &[u8]) -> 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: &[u8]) -> [u32; 8] { // Fill constants array with values const K: [u32; 64] = [ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, ]; let mut hash = init_hash_value; // 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 let mut t1: u32; let mut t2: u32; // Fill first 16 Byte of the word vector with the chunk values for t in 0..16 { w.push(chunk[t]); } // Fill the remaining bytes with the calculated values from the word vector for t in 16..64 { w.push( ssig1(w[t - 2]) .wrapping_add(w[t - 7]) .wrapping_add(ssig0(w[t - 15])) .wrapping_add(w[t - 16]), ); } // Fill swapping variables with corresponding values. let mut a = hash[0]; let mut b = hash[1]; let mut c = hash[2]; let mut d = hash[3]; let mut e = hash[4]; let mut f = hash[5]; let mut g = hash[6]; let mut h = hash[7]; // Do the required shuffling while mixing in the word values from the message for t in 0..64 { t1 = h .wrapping_add(bsig1(e)) .wrapping_add(ch(e, f, g)) .wrapping_add(K[t]) .wrapping_add(w[t]); t2 = bsig0(a).wrapping_add(maj(a, b, c)); h = g; g = f; f = e; e = d.wrapping_add(t1); d = c; c = b; b = a; a = t1.wrapping_add(t2); } // Add the swapping variable back into the hash array hash[0] = a.wrapping_add(hash[0]); hash[1] = b.wrapping_add(hash[1]); hash[2] = c.wrapping_add(hash[2]); hash[3] = d.wrapping_add(hash[3]); hash[4] = e.wrapping_add(hash[4]); 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 return hash; }