1pub const DIGEST_LEN: usize = 20;
21
22const INITIAL: [u32; 5] = [
24 0x6745_2301,
25 0xEFCD_AB89,
26 0x98BA_DCFE,
27 0x1032_5476,
28 0xC3D2_E1F0,
29];
30
31#[derive(Clone, Debug)]
34pub struct Sha1 {
35 state: [u32; 5],
36 block: [u8; 64],
38 filled: usize,
40 length: u64,
42}
43
44impl Default for Sha1 {
45 fn default() -> Self {
46 Self::new()
47 }
48}
49
50impl Sha1 {
51 pub fn new() -> Self {
53 Self {
54 state: INITIAL,
55 block: [0; 64],
56 filled: 0,
57 length: 0,
58 }
59 }
60
61 pub fn update(&mut self, mut data: &[u8]) {
63 self.length = self.length.wrapping_add(data.len() as u64);
64 if self.filled > 0 {
65 let take = data.len().min(64 - self.filled);
66 self.block[self.filled..self.filled + take].copy_from_slice(&data[..take]);
67 self.filled += take;
68 data = &data[take..];
69 if self.filled < 64 {
70 return;
71 }
72 compress(&mut self.state, &self.block);
73 self.filled = 0;
74 }
75 let (blocks, rest) = data.as_chunks::<64>();
76 for block in blocks {
77 compress(&mut self.state, block);
78 }
79 self.block[..rest.len()].copy_from_slice(rest);
80 self.filled = rest.len();
81 }
82
83 pub fn finish(mut self) -> [u8; DIGEST_LEN] {
86 let bits = self.length.wrapping_mul(8);
87 self.update(&[0x80]);
88 while self.filled != 56 {
89 self.update(&[0]);
90 }
91 self.update(&bits.to_be_bytes());
92 debug_assert_eq!(self.filled, 0, "the padding completes a block");
93 let mut digest = [0; DIGEST_LEN];
94 for (out, word) in digest.as_chunks_mut::<4>().0.iter_mut().zip(self.state) {
95 *out = word.to_be_bytes();
96 }
97 digest
98 }
99}
100
101pub fn sha1(data: &[u8]) -> [u8; DIGEST_LEN] {
103 let mut hasher = Sha1::new();
104 hasher.update(data);
105 hasher.finish()
106}
107
108fn compress(state: &mut [u32; 5], block: &[u8; 64]) {
110 let mut w = [0_u32; 80];
111 for (word, bytes) in w.iter_mut().zip(block.as_chunks::<4>().0) {
112 *word = u32::from_be_bytes(*bytes);
113 }
114 for t in 16..80 {
115 w[t] = (w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16]).rotate_left(1);
116 }
117 let [mut a, mut b, mut c, mut d, mut e] = *state;
118 for (t, &word) in w.iter().enumerate() {
119 let (f, k) = match t {
120 0..20 => ((b & c) | (!b & d), 0x5A82_7999),
121 20..40 => (b ^ c ^ d, 0x6ED9_EBA1),
122 40..60 => ((b & c) | (b & d) | (c & d), 0x8F1B_BCDC),
123 _ => (b ^ c ^ d, 0xCA62_C1D6),
124 };
125 let temp = a
126 .rotate_left(5)
127 .wrapping_add(f)
128 .wrapping_add(e)
129 .wrapping_add(word)
130 .wrapping_add(k);
131 e = d;
132 d = c;
133 c = b.rotate_left(30);
134 b = a;
135 a = temp;
136 }
137 for (word, value) in state.iter_mut().zip([a, b, c, d, e]) {
138 *word = word.wrapping_add(value);
139 }
140}
141
142#[cfg(test)]
143mod tests {
144 use std::fmt::Write as _;
145
146 use super::*;
147
148 fn hex(digest: [u8; DIGEST_LEN]) -> String {
149 let mut out = String::new();
150 for byte in digest {
151 write!(out, "{byte:02x}").unwrap();
152 }
153 out
154 }
155
156 #[test]
157 fn rfc_3174_test_vectors() {
158 assert_eq!(
159 hex(sha1(b"abc")),
160 "a9993e364706816aba3e25717850c26c9cd0d89d"
161 );
162 assert_eq!(
163 hex(sha1(
164 b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
165 )),
166 "84983e441c3bd26ebaae4aa1f95129e5e54670f1"
167 );
168 let mut hasher = Sha1::new();
169 for _ in 0..1_000_000 / 1000 {
170 hasher.update(&[b'a'; 1000]);
171 }
172 assert_eq!(
173 hex(hasher.finish()),
174 "34aa973cd4c4daa4f61eeb2bdbad27316534016f"
175 );
176 let mut hasher = Sha1::new();
177 for _ in 0..10 {
178 hasher.update(b"0123456701234567012345670123456701234567012345670123456701234567");
179 }
180 assert_eq!(
181 hex(hasher.finish()),
182 "dea356a2cddd90c7a7ecedc5ebb563934f460452"
183 );
184 }
185
186 #[test]
187 fn empty_and_block_boundaries() {
188 assert_eq!(hex(sha1(b"")), "da39a3ee5e6b4b0d3255bfef95601890afd80709");
189 let data: Vec<u8> = (0..=200_u8).collect();
192 for len in [55, 56, 63, 64, 65, 119, 120, 128, 200] {
193 let whole = sha1(&data[..len]);
194 let mut hasher = Sha1::new();
195 for byte in &data[..len] {
196 hasher.update(std::slice::from_ref(byte));
197 }
198 assert_eq!(hasher.finish(), whole, "length {len}");
199 }
200 assert_eq!(
201 hex(sha1(&[b'a'; 64])),
202 "0098ba824b5c16427bd7a1122a5a442a25ec644d"
203 );
204 }
205}