crc32_digest/
lib.rs

1use crc32fast::Hasher;
2use digest::{impl_write, FixedOutput, Input, Reset};
3use generic_array::typenum::U4;
4use generic_array::GenericArray;
5
6pub use digest::Digest;
7
8#[derive(Clone, Default)]
9/// Wraps a [`Hasher`] and provides it with [`Digest`] and [`DynDigest`] implementations.
10///
11/// [`Digest`]: ../digest/trait.Digest.html
12/// [`DynDigest`]: ../digest/trait.DynDigest.html
13/// [`Hasher`]: ../crc32fast/struct.Hasher.html
14pub struct Crc32(Hasher);
15
16impl Crc32 {
17    /// Creates a new `Crc32`.
18    #[inline]
19    pub fn new() -> Self {
20        Self(Hasher::new())
21    }
22
23    /// Creates a new `Crc32` initialized with the given state.
24    #[inline]
25    pub fn from_state(state: u32) -> Self {
26        Self(Hasher::new_with_initial(state))
27    }
28}
29
30impl FixedOutput for Crc32 {
31    type OutputSize = U4;
32
33    #[inline]
34    fn fixed_result(self) -> GenericArray<u8, Self::OutputSize> {
35        let result = self.0.finalize();
36        GenericArray::clone_from_slice(&result.to_be_bytes())
37    }
38}
39
40impl Input for Crc32 {
41    #[inline]
42    fn input<B: AsRef<[u8]>>(&mut self, data: B) {
43        self.0.update(data.as_ref());
44    }
45}
46
47impl Reset for Crc32 {
48    #[inline]
49    fn reset(&mut self) {
50        self.0.reset();
51    }
52}
53
54impl_write!(Crc32);