1const K: [u32; 64] = [
8 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
9 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
10 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
11 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
12 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
13 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
14 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
15 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
16];
17
18const H0: [u32; 8] = [
21 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
22];
23
24const BLOCK_SIZE: usize = 64;
25const LENGTH_OFFSET: usize = 56;
27
28pub struct Sha256 {
29 hash: [u32; 8],
30 buffer: [u8; BLOCK_SIZE],
32 buffered: usize,
33 total_bytes: u64,
34}
35
36impl Default for Sha256 {
37 fn default() -> Self {
38 Self::new()
39 }
40}
41
42impl Sha256 {
43 pub fn new() -> Sha256 {
44 Sha256 {
45 hash: H0,
46 buffer: [0u8; BLOCK_SIZE],
47 buffered: 0,
48 total_bytes: 0,
49 }
50 }
51
52 pub fn update(&mut self, mut data: &[u8]) {
54 self.total_bytes = self.total_bytes.wrapping_add(data.len() as u64);
55
56 if self.buffered > 0 {
57 let taken = (BLOCK_SIZE - self.buffered).min(data.len());
58 self.buffer[self.buffered..self.buffered + taken].copy_from_slice(&data[..taken]);
59 self.buffered += taken;
60 data = &data[taken..];
61 if self.buffered < BLOCK_SIZE {
62 return;
64 }
65 let block = self.buffer;
66 self.compress(&block);
67 self.buffered = 0;
68 }
69
70 let (blocks, rest) = data.as_chunks::<BLOCK_SIZE>();
71 for block in blocks {
72 self.compress(block);
73 }
74 self.buffer[..rest.len()].copy_from_slice(rest);
75 self.buffered = rest.len();
76 }
77
78 pub fn finalize(mut self) -> [u8; 32] {
80 let length_in_bits = self.total_bytes.wrapping_mul(8);
81 let mut padding = [0u8; BLOCK_SIZE + 8];
82 padding[0] = 0x80;
83 let zeros = if self.buffered < LENGTH_OFFSET {
84 LENGTH_OFFSET - self.buffered
85 } else {
86 BLOCK_SIZE + LENGTH_OFFSET - self.buffered
87 };
88 padding[zeros..zeros + 8].copy_from_slice(&length_in_bits.to_be_bytes());
89 self.update(&padding[..zeros + 8]);
90 debug_assert_eq!(self.buffered, 0, "padding must end on a block boundary");
91
92 let mut digest = [0u8; 32];
93 for (index, word) in self.hash.iter().enumerate() {
94 digest[index * 4..index * 4 + 4].copy_from_slice(&word.to_be_bytes());
95 }
96 digest
97 }
98
99 fn compress(&mut self, block: &[u8; BLOCK_SIZE]) {
101 let mut w = [0u32; 64];
102 for (i, word) in w.iter_mut().take(16).enumerate() {
103 *word = u32::from_be_bytes([
104 block[i * 4],
105 block[i * 4 + 1],
106 block[i * 4 + 2],
107 block[i * 4 + 3],
108 ]);
109 }
110 for i in 16..64 {
111 let s0 = w[i - 15].rotate_right(7) ^ w[i - 15].rotate_right(18) ^ (w[i - 15] >> 3);
112 let s1 = w[i - 2].rotate_right(17) ^ w[i - 2].rotate_right(19) ^ (w[i - 2] >> 10);
113 w[i] = w[i - 16]
114 .wrapping_add(s0)
115 .wrapping_add(w[i - 7])
116 .wrapping_add(s1);
117 }
118
119 let [mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut h] = self.hash;
120 for i in 0..64 {
121 let s1 = e.rotate_right(6) ^ e.rotate_right(11) ^ e.rotate_right(25);
122 let ch = (e & f) ^ ((!e) & g);
123 let t1 = h
124 .wrapping_add(s1)
125 .wrapping_add(ch)
126 .wrapping_add(K[i])
127 .wrapping_add(w[i]);
128 let s0 = a.rotate_right(2) ^ a.rotate_right(13) ^ a.rotate_right(22);
129 let maj = (a & b) ^ (a & c) ^ (b & c);
130 let t2 = s0.wrapping_add(maj);
131
132 h = g;
133 g = f;
134 f = e;
135 e = d.wrapping_add(t1);
136 d = c;
137 c = b;
138 b = a;
139 a = t1.wrapping_add(t2);
140 }
141 for (slot, value) in self.hash.iter_mut().zip([a, b, c, d, e, f, g, h]) {
142 *slot = slot.wrapping_add(value);
143 }
144 }
145}
146
147pub fn hex(digest: &[u8; 32]) -> String {
149 digest.iter().map(|byte| format!("{byte:02x}")).collect()
150}
151
152pub fn hex_digest(data: &[u8]) -> String {
154 let mut hasher = Sha256::new();
155 hasher.update(data);
156 hex(&hasher.finalize())
157}
158
159pub fn hex_digest_of_file(path: &std::path::Path) -> std::io::Result<String> {
161 let mut file = std::fs::File::open(path)?;
162 let mut hasher = Sha256::new();
163 let mut buffer = vec![0u8; 64 * 1024];
164 loop {
165 let read = std::io::Read::read(&mut file, &mut buffer)?;
166 if read == 0 {
167 break;
168 }
169 hasher.update(&buffer[..read]);
170 }
171 Ok(hex(&hasher.finalize()))
172}
173
174#[cfg(test)]
175mod tests;