Rust implementation of CRC(16, 32, 64) with support of various standards
Add crc
to Cargo.toml
toml
[dependencies]
crc = "^1.0.0"
or
toml
[dependencies.crc]
git = "https://github.com/mrhooray/crc-rs"
Add this to crate root
rust
extern crate crc;
```rust use crc::{crc16, Hasher16};
asserteq!(crc16::checksumx25(b"123456789"), 0x906e); asserteq!(crc16::checksumusb(b"123456789"), 0xb4c8);
// use provided or custom polynomial let mut digest = crc16::Digest::new(crc16::X25); digest.write(b"123456789"); assert_eq!(digest.sum16(), 0x906e);
// with initial let mut digest = crc16::Digest::newwithinitial(crc16::X25, 0u16); digest.write(b"123456789"); assert_eq!(digest.sum16(), 0x906e); ```
```rust use crc::{crc32, Hasher32};
// CRC-32-IEEE being the most commonly used one asserteq!(crc32::checksumieee(b"123456789"), 0xcbf43926); asserteq!(crc32::checksumcastagnoli(b"123456789"), 0xe3069283); asserteq!(crc32::checksumkoopman(b"123456789"), 0x2d3dd0ae);
// use provided or custom polynomial let mut digest = crc32::Digest::new(crc32::IEEE); digest.write(b"123456789"); assert_eq!(digest.sum32(), 0xcbf43926);
// with initial let mut digest = crc32::Digest::newwithinitial(crc32::IEEE, 0u32); digest.write(b"123456789"); assert_eq!(digest.sum32(), 0xcbf43926); ```
```rust use crc::{crc64, Hasher64};
asserteq!(crc64::checksumecma(b"123456789"), 0x995dc9bbdf1939fa); asserteq!(crc64::checksumiso(b"123456789"), 0xb90956c775a41001);
// use provided or custom polynomial let mut digest = crc64::Digest::new(crc64::ECMA); digest.write(b"123456789"); assert_eq!(digest.sum64(), 0x995dc9bbdf1939fa);
// with initial let mut digest = crc64::Digest::newwithinitial(crc64::ECMA, 0u64); digest.write(b"123456789"); assert_eq!(digest.sum64(), 0x995dc9bbdf1939fa); ```
Bencher is currently not available in Rust stable releases.
cargo bench
with 2.3 GHz Intel Core i7 results ~430MB/s throughput. Comparison
```
cargo bench
Running target/release/bench-5c82e94dab3e9c79
running 4 tests test benchcrc32maketable ... bench: 439 ns/iter (+/- 82) test benchcrc32updatemegabytes ... bench: 2327803 ns/iter (+/- 138845) test benchcrc64maketable ... bench: 1200 ns/iter (+/- 223) test benchcrc64updatemegabytes ... bench: 2322472 ns/iter (+/- 92870)
test result: ok. 0 passed; 0 failed; 0 ignored; 4 measured ```
Licensed under either of
at your option.
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.