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