1#![cfg_attr(not(feature = "std"), no_std)]
41#![deny(missing_docs)]
42#![deny(unsafe_code)]
43use core::fmt;
44use core::hash;
45
46mod baseline;
47mod combine;
48mod specialized;
49mod table;
50
51#[inline]
55pub fn hash(buf: &[u8]) -> u32 {
56 let mut h = Hasher::new();
57 h.update(buf);
58 h.finalize()
59}
60
61#[derive(Clone)]
62pub struct Hasher {
64 amount: u64,
65 state: u32,
66 spec: Option<specialized::State>,
67}
68
69const DEFAULT_INIT_STATE: u32 = 0;
70
71impl Hasher {
72 #[inline]
77 pub fn new() -> Self {
78 Self::new_with_initial(DEFAULT_INIT_STATE)
79 }
80
81 #[inline]
86 pub fn new_with_initial(init: u32) -> Self {
87 Self::new_with_initial_len(init, 0)
88 }
89
90 #[inline]
96 pub fn new_with_initial_len(init: u32, amount: u64) -> Self {
97 Self::internal_new_specialized(init, amount)
98 .unwrap_or_else(|| Self::internal_new_baseline(init, amount))
99 }
100
101 #[doc(hidden)]
102 #[inline]
104 pub fn internal_new_baseline(init: u32, amount: u64) -> Self {
105 Hasher {
106 amount,
107 state: init,
108 spec: None,
109 }
110 }
111
112 #[doc(hidden)]
113 #[inline]
115 pub fn internal_new_specialized(init: u32, amount: u64) -> Option<Self> {
116 Some(Hasher {
117 amount,
118 state: init,
119 spec: Some(specialized::State::new()?),
120 })
121 }
122
123 #[inline]
125 pub fn update(&mut self, buf: &[u8]) {
126 self.amount += buf.len() as u64;
127 self.state = match &self.spec {
128 None => baseline::update_fast_16(self.state, buf),
129 Some(spec) => spec.update(self.state, buf),
130 };
131 }
132
133 #[inline]
151 pub fn total_len(&self) -> u64 {
152 self.amount
153 }
154
155 #[inline]
157 pub fn finalize(self) -> u32 {
158 self.state
159 }
160
161 #[inline]
163 pub fn reset(&mut self) {
164 self.state = 0;
165 self.amount = 0;
166 }
167
168 pub fn combine(&mut self, other: &Self) {
170 self.amount += other.amount;
171 self.state = combine::combine(self.state, other.state, other.amount);
172 }
173}
174
175impl fmt::Debug for Hasher {
176 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
177 f.debug_struct("crc32fast::Hasher").finish()
178 }
179}
180
181impl Default for Hasher {
182 fn default() -> Self {
183 Self::new()
184 }
185}
186
187impl hash::Hasher for Hasher {
188 fn write(&mut self, bytes: &[u8]) {
189 self.update(bytes)
190 }
191
192 fn finish(&self) -> u64 {
193 u64::from(self.clone().finalize())
194 }
195}
196
197#[cfg(test)]
198mod test {
199 use super::Hasher;
200
201 #[test]
202 fn total_len_tracks_resumed_and_combined_blocks() {
203 let prefix = b"hello";
204 let suffix = b" world";
205 let mut hasher = Hasher::new_with_initial_len(super::hash(prefix), prefix.len() as u64);
206 assert_eq!(hasher.total_len(), 5);
207 hasher.update(b" ");
208 assert_eq!(hasher.total_len(), 6);
209
210 let mut remaining = Hasher::new();
211 remaining.update(&suffix[1..]);
212 hasher.combine(&remaining);
213 assert_eq!(hasher.total_len(), 11);
214 assert_eq!(hasher.clone().finalize(), super::hash(b"hello world"));
215 assert_eq!(remaining.total_len(), 5);
216
217 hasher.reset();
218 assert_eq!(hasher.total_len(), 0);
219 hasher.update(b"again");
220 assert_eq!(hasher.total_len(), 5);
221 assert_eq!(hasher.finalize(), super::hash(b"again"));
222 }
223
224 #[test]
225 fn total_len_for_empty_and_unknown_initial_length() {
226 let mut hasher = Hasher::new();
227 assert_eq!(hasher.total_len(), 0);
228 hasher.update(b"");
229 hasher.combine(&Hasher::new());
230 assert_eq!(hasher.total_len(), 0);
231 assert_eq!(
232 Hasher::new_with_initial(super::hash(b"prefix")).total_len(),
233 0
234 );
235 assert_eq!(
236 Hasher::new_with_initial_len(0, u64::MAX).total_len(),
237 u64::MAX
238 );
239 }
240
241 quickcheck::quickcheck! {
242 fn combine(bytes_1: Vec<u8>, bytes_2: Vec<u8>) -> bool {
243 let mut hash_a = Hasher::new();
244 hash_a.update(&bytes_1);
245 hash_a.update(&bytes_2);
246 let mut hash_b = Hasher::new();
247 hash_b.update(&bytes_2);
248 let mut hash_c = Hasher::new();
249 hash_c.update(&bytes_1);
250 hash_c.combine(&hash_b);
251
252 hash_a.finalize() == hash_c.finalize()
253 }
254
255 fn combine_from_len(bytes_1: Vec<u8>, bytes_2: Vec<u8>) -> bool {
256 let mut hash_a = Hasher::new();
257 hash_a.update(&bytes_1);
258
259 let mut hash_b = Hasher::new();
260 hash_b.update(&bytes_2);
261
262 let mut hash_ab = Hasher::new();
263 hash_ab.update(&bytes_1);
264 hash_ab.update(&bytes_2);
265 let ab = hash_ab.finalize();
266
267 hash_a.combine(&hash_b);
268 hash_a.finalize() == ab
269 }
270 }
271}