diff --git a/src/main.rs b/src/main.rs index e7a11a9..6c3eeb0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,18 @@ +mod sha256; + fn main() { - println!("Hello, world!"); + + println!("Input is: \"\""); + println!("Expected is: 0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); + sha256::sha256(""); + + println!("##########################################"); + println!("Input is: \"abc\""); + println!("Expected is: 0xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); + sha256::sha256("abc"); + + println!("##########################################"); + println!("Input is: \"The quick brown fox jumps over the lazy dog\""); + println!("Expected is: 0xd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"); + sha256::sha256("The quick brown fox jumps over the lazy dog"); } diff --git a/src/sha256.rs b/src/sha256.rs new file mode 100644 index 0000000..798ff99 --- /dev/null +++ b/src/sha256.rs @@ -0,0 +1,146 @@ +use std::str; + +//////////// 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(message: &str) -> Vec { + let mut msg_bytes = message.as_bytes().to_vec(); + let l = (msg_bytes.len() as u64) * 8; + + msg_bytes.push(0x80); + + let k = (448u64.wrapping_sub((l + 8) % 512)) % 512; + let k_bytes = k / 8; + + let padding = vec![0u8; k_bytes as usize]; + + msg_bytes.extend(padding); + + msg_bytes.extend(&l.to_be_bytes()); + + let mut u32_vec = Vec::with_capacity(msg_bytes.len() / 4); // Allocate the right size for u32 vector + + for chunk in msg_bytes.chunks(4) { + let value = (chunk[0] as u32) << 24 + | (chunk[1] as u32) << 16 + | (chunk[2] as u32) << 8 + | (chunk[3] as u32); + u32_vec.push(value); + } + + return u32_vec; +} +//////////// END HELPER FUNCTIONS ///////////// + +pub fn sha256(message: &str) { + 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: [u32; 8] = [ + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, + 0x5be0cd19, + ]; + + let msg_vec = pad_message(message); + + for chunk in msg_vec.chunks(64) { + let mut w: Vec = Vec::with_capacity(64); + let mut t1: u32; + let mut t2: u32; + + for t in 0..16 { + w.push(chunk[t]); + } + + 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]), + ); + } + + 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]; + + 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); + } + + 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]); + } + + let sha256_str: String = hash + .iter() + .map(|&num| format!("{:X}", num)) // Use {:X} for uppercase hex, or {:x} for lowercase + .collect::>() // Collect into a vector of strings + .join(""); // Join with a space or any separator you prefer + + println!("Result Hash is: 0x{}", sha256_str); + +}