diff --git a/src/crc128.rs b/src/crc128.rs index 7768b0d..f18a98f 100644 --- a/src/crc128.rs +++ b/src/crc128.rs @@ -8,16 +8,16 @@ impl Crc { } pub const fn checksum(&self, bytes: &[u8]) -> u128 { - let mut crc = self.init(); + let mut crc = self.init(self.algorithm.init); crc = self.update(crc, bytes); self.finalize(crc) } - const fn init(&self) -> u128 { + const fn init(&self, initial: u128) -> u128 { if self.algorithm.refin { - self.algorithm.init.reverse_bits() >> (128u8 - self.algorithm.width) + initial.reverse_bits() >> (128u8 - self.algorithm.width) } else { - self.algorithm.init << (128u8 - self.algorithm.width) + initial << (128u8 - self.algorithm.width) } } @@ -54,13 +54,22 @@ impl Crc { } pub const fn digest(&self) -> Digest { - Digest::new(self) + self.digest_with_initial(self.algorithm.init) + } + + /// Construct a `Digest` with a given initial value. + /// + /// This overrides the initial value specified by the algorithm. + /// The effects of the algorithm's properties `refin` and `width` + /// are applied to the custom initial value. + pub const fn digest_with_initial(&self, initial: u128) -> Digest { + let value = self.init(initial); + Digest::new(self, value) } } impl<'a> Digest<'a, u128> { - const fn new(crc: &'a Crc) -> Self { - let value = crc.init(); + const fn new(crc: &'a Crc, value: u128) -> Self { Digest { crc, value } } diff --git a/src/crc16.rs b/src/crc16.rs index 4397f2d..9b28a45 100644 --- a/src/crc16.rs +++ b/src/crc16.rs @@ -8,16 +8,16 @@ impl Crc { } pub const fn checksum(&self, bytes: &[u8]) -> u16 { - let mut crc = self.init(); + let mut crc = self.init(self.algorithm.init); crc = self.update(crc, bytes); self.finalize(crc) } - const fn init(&self) -> u16 { + const fn init(&self, initial: u16) -> u16 { if self.algorithm.refin { - self.algorithm.init.reverse_bits() >> (16u8 - self.algorithm.width) + initial.reverse_bits() >> (16u8 - self.algorithm.width) } else { - self.algorithm.init << (16u8 - self.algorithm.width) + initial << (16u8 - self.algorithm.width) } } @@ -54,13 +54,22 @@ impl Crc { } pub const fn digest(&self) -> Digest { - Digest::new(self) + self.digest_with_initial(self.algorithm.init) + } + + /// Construct a `Digest` with a given initial value. + /// + /// This overrides the initial value specified by the algorithm. + /// The effects of the algorithm's properties `refin` and `width` + /// are applied to the custom initial value. + pub const fn digest_with_initial(&self, initial: u16) -> Digest { + let value = self.init(initial); + Digest::new(self, value) } } impl<'a> Digest<'a, u16> { - const fn new(crc: &'a Crc) -> Self { - let value = crc.init(); + const fn new(crc: &'a Crc, value: u16) -> Self { Digest { crc, value } } diff --git a/src/crc32.rs b/src/crc32.rs index 0b6c7ca..580403b 100644 --- a/src/crc32.rs +++ b/src/crc32.rs @@ -8,16 +8,16 @@ impl Crc { } pub const fn checksum(&self, bytes: &[u8]) -> u32 { - let mut crc = self.init(); + let mut crc = self.init(self.algorithm.init); crc = self.update(crc, bytes); self.finalize(crc) } - const fn init(&self) -> u32 { + const fn init(&self, initial: u32) -> u32 { if self.algorithm.refin { - self.algorithm.init.reverse_bits() >> (32u8 - self.algorithm.width) + initial.reverse_bits() >> (32u8 - self.algorithm.width) } else { - self.algorithm.init << (32u8 - self.algorithm.width) + initial << (32u8 - self.algorithm.width) } } @@ -54,13 +54,22 @@ impl Crc { } pub const fn digest(&self) -> Digest { - Digest::new(self) + self.digest_with_initial(self.algorithm.init) + } + + /// Construct a `Digest` with a given initial value. + /// + /// This overrides the initial value specified by the algorithm. + /// The effects of the algorithm's properties `refin` and `width` + /// are applied to the custom initial value. + pub const fn digest_with_initial(&self, initial: u32) -> Digest { + let value = self.init(initial); + Digest::new(self, value) } } impl<'a> Digest<'a, u32> { - const fn new(crc: &'a Crc) -> Self { - let value = crc.init(); + const fn new(crc: &'a Crc, value: u32) -> Self { Digest { crc, value } } diff --git a/src/crc64.rs b/src/crc64.rs index d7a0b73..45b0211 100644 --- a/src/crc64.rs +++ b/src/crc64.rs @@ -8,16 +8,16 @@ impl Crc { } pub const fn checksum(&self, bytes: &[u8]) -> u64 { - let mut crc = self.init(); + let mut crc = self.init(self.algorithm.init); crc = self.update(crc, bytes); self.finalize(crc) } - const fn init(&self) -> u64 { + const fn init(&self, initial: u64) -> u64 { if self.algorithm.refin { - self.algorithm.init.reverse_bits() >> (64u8 - self.algorithm.width) + initial.reverse_bits() >> (64u8 - self.algorithm.width) } else { - self.algorithm.init << (64u8 - self.algorithm.width) + initial << (64u8 - self.algorithm.width) } } @@ -54,13 +54,22 @@ impl Crc { } pub const fn digest(&self) -> Digest { - Digest::new(self) + self.digest_with_initial(self.algorithm.init) + } + + /// Construct a `Digest` with a given initial value. + /// + /// This overrides the initial value specified by the algorithm. + /// The effects of the algorithm's properties `refin` and `width` + /// are applied to the custom initial value. + pub const fn digest_with_initial(&self, initial: u64) -> Digest { + let value = self.init(initial); + Digest::new(self, value) } } impl<'a> Digest<'a, u64> { - const fn new(crc: &'a Crc) -> Self { - let value = crc.init(); + const fn new(crc: &'a Crc, value: u64) -> Self { Digest { crc, value } } diff --git a/src/crc8.rs b/src/crc8.rs index 9c56a33..2531b3e 100644 --- a/src/crc8.rs +++ b/src/crc8.rs @@ -8,16 +8,16 @@ impl Crc { } pub const fn checksum(&self, bytes: &[u8]) -> u8 { - let mut crc = self.init(); + let mut crc = self.init(self.algorithm.init); crc = self.update(crc, bytes); self.finalize(crc) } - const fn init(&self) -> u8 { + const fn init(&self, initial: u8) -> u8 { if self.algorithm.refin { - self.algorithm.init.reverse_bits() >> (8u8 - self.algorithm.width) + initial.reverse_bits() >> (8u8 - self.algorithm.width) } else { - self.algorithm.init << (8u8 - self.algorithm.width) + initial << (8u8 - self.algorithm.width) } } @@ -47,13 +47,22 @@ impl Crc { } pub const fn digest(&self) -> Digest { - Digest::new(self) + self.digest_with_initial(self.algorithm.init) + } + + /// Construct a `Digest` with a given initial value. + /// + /// This overrides the initial value specified by the algorithm. + /// The effects of the algorithm's properties `refin` and `width` + /// are applied to the custom initial value. + pub const fn digest_with_initial(&self, initial: u8) -> Digest { + let value = self.init(initial); + Digest::new(self, value) } } impl<'a> Digest<'a, u8> { - const fn new(crc: &'a Crc) -> Self { - let value = crc.init(); + const fn new(crc: &'a Crc, value: u8) -> Self { Digest { crc, value } }