@@ -376,6 +376,18 @@ impl DynDigest for Digest {
376376
377377impl Digest {
378378 /// Creates a new `Digest` instance for the specified CRC algorithm.
379+ ///
380+ /// # Examples
381+ ///
382+ /// ```rust
383+ /// use crc_fast::{Digest, CrcAlgorithm::Crc32IsoHdlc};
384+ ///
385+ /// let mut digest = Digest::new(Crc32IsoHdlc);
386+ /// digest.update(b"123456789");
387+ /// let checksum = digest.finalize();
388+ ///
389+ /// assert_eq!(checksum, 0xcbf43926);
390+ /// ```
379391 #[ inline( always) ]
380392 pub fn new ( algorithm : CrcAlgorithm ) -> Self {
381393 let ( calculator, params) = get_calculator_params ( algorithm) ;
@@ -388,6 +400,42 @@ impl Digest {
388400 }
389401 }
390402
403+ /// Creates a new `Digest` instance for the specified CRC algorithm with a custom initial state.
404+ ///
405+ /// # Examples
406+ ///
407+ /// ```rust
408+ /// use crc_fast::{Digest, CrcAlgorithm::Crc32IsoHdlc};
409+ ///
410+ /// // CRC-32/ISO-HDLC with initial state of 0x00000000, instead of the default initial state
411+ /// // of 0xffffffff,
412+ /// let mut digest = Digest::new_with_init_state(Crc32IsoHdlc, 0x00000000);
413+ /// digest.update(b"123456789");
414+ /// let checksum = digest.finalize();
415+ ///
416+ /// // different initial state, so checksum will be different
417+ /// assert_eq!(checksum, 0xd202d277);
418+ ///
419+ /// let mut digest = Digest::new_with_init_state(Crc32IsoHdlc, 0xffffffff);
420+ /// digest.update(b"123456789");
421+ /// let checksum = digest.finalize();
422+ ///
423+ /// // same initial state as the default, so checksum will be the same
424+ /// assert_eq!(checksum, 0xcbf43926);
425+ /// ```
426+ #[ inline( always) ]
427+ pub fn new_with_init_state ( algorithm : CrcAlgorithm , init_state : u64 ) -> Self {
428+ let ( calculator, params) = get_calculator_params ( algorithm) ;
429+
430+ Self {
431+ state : init_state,
432+ amount : 0 ,
433+ params,
434+ calculator,
435+ }
436+ }
437+
438+
391439 /// Creates a new `Digest` instance with custom CRC parameters.
392440 ///
393441 /// # Examples
@@ -474,6 +522,27 @@ impl Digest {
474522 pub fn get_amount ( & self ) -> u64 {
475523 self . amount
476524 }
525+
526+ /// Gets the current CRC state.
527+ ///
528+ /// # Examples
529+ /// ```rust
530+ /// use crc_fast::{Digest, CrcAlgorithm::Crc32IsoHdlc};
531+ ///
532+ /// let mut digest = Digest::new(Crc32IsoHdlc);
533+ /// digest.update(b"123456789");
534+ /// let state = digest.get_state();
535+ ///
536+ /// // non-finalized state, so it won't match the final checksum
537+ /// assert_eq!(state, 0x340bc6d9);
538+ ///
539+ /// // finalized state will match the checksum
540+ /// assert_eq!(digest.finalize(), 0xcbf43926);
541+ /// ```
542+ #[ inline( always) ]
543+ pub fn get_state ( & self ) -> u64 {
544+ self . state
545+ }
477546}
478547
479548impl Write for Digest {
0 commit comments