@@ -376,6 +376,18 @@ impl DynDigest for Digest {
376
376
377
377
impl Digest {
378
378
/// 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
+ /// ```
379
391
#[ inline( always) ]
380
392
pub fn new ( algorithm : CrcAlgorithm ) -> Self {
381
393
let ( calculator, params) = get_calculator_params ( algorithm) ;
@@ -388,6 +400,41 @@ impl Digest {
388
400
}
389
401
}
390
402
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
+
391
438
/// Creates a new `Digest` instance with custom CRC parameters.
392
439
///
393
440
/// # Examples
@@ -474,6 +521,27 @@ impl Digest {
474
521
pub fn get_amount ( & self ) -> u64 {
475
522
self . amount
476
523
}
524
+
525
+ /// Gets the current CRC state.
526
+ ///
527
+ /// # Examples
528
+ /// ```rust
529
+ /// use crc_fast::{Digest, CrcAlgorithm::Crc32IsoHdlc};
530
+ ///
531
+ /// let mut digest = Digest::new(Crc32IsoHdlc);
532
+ /// digest.update(b"123456789");
533
+ /// let state = digest.get_state();
534
+ ///
535
+ /// // non-finalized state, so it won't match the final checksum
536
+ /// assert_eq!(state, 0x340bc6d9);
537
+ ///
538
+ /// // finalized state will match the checksum
539
+ /// assert_eq!(digest.finalize(), 0xcbf43926);
540
+ /// ```
541
+ #[ inline( always) ]
542
+ pub fn get_state ( & self ) -> u64 {
543
+ self . state
544
+ }
477
545
}
478
546
479
547
impl Write for Digest {
0 commit comments