#crc32 #hashing #hash #crc16 #crc64

no-std crc

Rust implementation of CRC with support of various standards

22 releases (stable)

3.2.1 Apr 8, 2024
3.1.0 Mar 1, 2024
3.1.0-beta.1 Oct 31, 2023
3.0.1 Jan 28, 2023
0.2.0 Mar 17, 2015

#6 in Algorithms

Download history 606500/week @ 2024-08-22 581756/week @ 2024-08-29 621637/week @ 2024-09-05 570486/week @ 2024-09-12 570172/week @ 2024-09-19 599994/week @ 2024-09-26 622180/week @ 2024-10-03 635176/week @ 2024-10-10 683614/week @ 2024-10-17 708042/week @ 2024-10-24 651503/week @ 2024-10-31 672554/week @ 2024-11-07 699375/week @ 2024-11-14 652921/week @ 2024-11-21 579756/week @ 2024-11-28 544143/week @ 2024-12-05

2,598,762 downloads per month
Used in 2,021 crates (368 directly)

MIT/Apache

76KB
2K SLoC

crc

Rust implementation of CRC.

ci Crate Docs License

Usage

Add crc to Cargo.toml

[dependencies]
crc = "3.2.1"

Examples

Using a well-known algorithm:

const X25: crc::Crc<u16> = crc::Crc::<u16>::new(&crc::CRC_16_IBM_SDLC);
assert_eq!(X25.checksum(b"123456789"), 0x906e);

Using a custom algorithm:

const CUSTOM_ALG: crc::Algorithm<u16> = crc::Algorithm {
    width: 16,
    poly: 0x8005,
    init: 0xffff,
    refin: false,
    refout: false,
    xorout: 0x0000,
    check: 0xaee7,
    residue: 0x0000
};
let crc = crc::Crc::<u16>::new(&CUSTOM_ALG);
let mut digest = crc.digest();
digest.update(b"123456789");
assert_eq!(digest.finalize(), 0xaee7);

Minimum supported Rust version (MSRV)

This crate's MSRV is 1.65.

At a minimum, the MSRV will be <= the oldest stable release in the last 12 months. MSRV may be bumped in minor version releases.

Implementations

This crate has several pluggable implementations:

  1. NoTable doesn't use a lookup table, and thus minimizes binary size and memory usage.
  2. Table<1> uses a lookup table with 256 entries (e.g. for u32 thats 256 * 4 bytes).
  3. Table<16> uses a lookup table with 16 * 256 entries (e.g. for u32 thats 16 * 256 * 4 bytes).

Table<1> is the default implementation, but this can be overridden by specifying I in Crc<W, I>. E.g.: Crc<u32, NoTable>, Crc<u64, Table<16>>, ...

NOTE: Lookup tables will increase binary size if they're generated at compile-time. Wrapping Crc initialization in a std::cell::OnceCell may be preferable if binary size is a concern.

Benchmark

cargo bench with AMD Ryzen 7 3800X (comparison).

Throughput (GiB/s)

Width NoTable Bytewise Slice16
8 0.113 0.585 3.11
16 0.105 0.483 3.23
32 0.111 0.516 3.30
64 0.139 0.517 2.92
82 0.091 0.438 0.623

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~26KB