1#![cfg_attr(not(feature = "std"), no_std)]
38
39use core::{fmt, hash};
45#[cfg(feature = "std")]
46use std as core;
47
48mod baseline;
49mod combine;
50mod specialized;
51mod table;
52
53pub fn hash(buf: &[u8]) -> u32 {
57 let mut h = Hasher::new();
58 h.update(buf);
59 h.finalize()
60}
61
62#[derive(Clone)]
63enum State {
64 Baseline(baseline::State),
65 Specialized(specialized::State),
66}
67
68#[derive(Clone)]
69pub struct Hasher {
71 amount: u64,
72 state: State,
73}
74
75const DEFAULT_INIT_STATE: u32 = 0;
76
77impl Hasher {
78 pub fn new() -> Self {
83 Self::new_with_initial(DEFAULT_INIT_STATE)
84 }
85
86 pub fn new_with_initial(init: u32) -> Self {
91 Self::new_with_initial_len(init, 0)
92 }
93
94 pub fn new_with_initial_len(init: u32, amount: u64) -> Self {
100 Self::internal_new_specialized(init, amount).unwrap_or_else(|| Self::internal_new_baseline(init, amount))
101 }
102
103 #[doc(hidden)]
104 pub fn internal_new_baseline(init: u32, amount: u64) -> Self {
106 Hasher {
107 amount,
108 state: State::Baseline(baseline::State::new(init)),
109 }
110 }
111
112 #[doc(hidden)]
113 pub fn internal_new_specialized(init: u32, amount: u64) -> Option<Self> {
115 {
116 if let Some(state) = specialized::State::new(init) {
117 return Some(Hasher {
118 amount,
119 state: State::Specialized(state),
120 });
121 }
122 }
123 None
124 }
125
126 pub fn update(&mut self, buf: &[u8]) {
128 self.amount += buf.len() as u64;
129 match self.state {
130 State::Baseline(ref mut state) => state.update(buf),
131 State::Specialized(ref mut state) => state.update(buf),
132 }
133 }
134
135 pub fn finalize(self) -> u32 {
137 match self.state {
138 State::Baseline(state) => state.finalize(),
139 State::Specialized(state) => state.finalize(),
140 }
141 }
142
143 pub fn reset(&mut self) {
145 self.amount = 0;
146 match self.state {
147 State::Baseline(ref mut state) => state.reset(),
148 State::Specialized(ref mut state) => state.reset(),
149 }
150 }
151
152 pub fn combine(&mut self, other: &Self) {
154 self.amount += other.amount;
155 let other_crc = other.clone().finalize();
156 match self.state {
157 State::Baseline(ref mut state) => state.combine(other_crc, other.amount),
158 State::Specialized(ref mut state) => state.combine(other_crc, other.amount),
159 }
160 }
161}
162
163impl fmt::Debug for Hasher {
164 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
165 f.debug_struct("crc32::Hasher").finish()
166 }
167}
168
169impl Default for Hasher {
170 fn default() -> Self {
171 Self::new()
172 }
173}
174
175impl hash::Hasher for Hasher {
176 fn write(&mut self, bytes: &[u8]) {
177 self.update(bytes)
178 }
179
180 fn finish(&self) -> u64 {
181 u64::from(self.clone().finalize())
182 }
183}
184
185#[cfg(test)]
186mod test {
187 use rand::{TryRng, rngs::SysRng};
188
189 use super::Hasher;
190
191 #[test]
192 fn combine() {
193 let mut rand_generator = SysRng;
194
195 let mut bytes_1 = vec![100];
196 rand_generator.try_fill_bytes(&mut bytes_1).unwrap();
197
198 let mut bytes_2 = vec![200];
199 rand_generator.try_fill_bytes(&mut bytes_2).unwrap();
200
201 let mut hash_a = Hasher::new();
202 hash_a.update(&bytes_1);
203 hash_a.update(&bytes_2);
204 let mut hash_b = Hasher::new();
205 hash_b.update(&bytes_2);
206 let mut hash_c = Hasher::new();
207 hash_c.update(&bytes_1);
208 hash_c.combine(&hash_b);
209
210 assert_eq!(hash_a.finalize(), hash_c.finalize());
211 }
212
213 #[test]
214 fn combine_from_len() {
215 let mut rand_generator = SysRng;
216
217 let mut bytes_1 = vec![200];
218 rand_generator.try_fill_bytes(&mut bytes_1).unwrap();
219
220 let mut bytes_2 = vec![100];
221 rand_generator.try_fill_bytes(&mut bytes_2).unwrap();
222
223 let mut hash_a = Hasher::new();
224 hash_a.update(&bytes_1);
225 let a = hash_a.finalize();
226
227 let mut hash_b = Hasher::new();
228 hash_b.update(&bytes_2);
229 let b = hash_b.finalize();
230
231 let mut hash_ab = Hasher::new();
232 hash_ab.update(&bytes_1);
233 hash_ab.update(&bytes_2);
234 let ab = hash_ab.finalize();
235
236 let mut reconstructed = Hasher::new_with_initial_len(a, bytes_1.len() as u64);
237 let hash_b_reconstructed = Hasher::new_with_initial_len(b, bytes_2.len() as u64);
238
239 reconstructed.combine(&hash_b_reconstructed);
240
241 assert_eq!(reconstructed.finalize(), ab);
242 }
243}