5 releases

0.0.4 Dec 3, 2023
0.0.3 Nov 16, 2023
0.0.2 Nov 16, 2023
0.0.1 Nov 16, 2023
0.0.0 Nov 16, 2023

#1046 in Algorithms

Download history 12/week @ 2024-07-23 1/week @ 2024-09-03 7/week @ 2024-09-10 62/week @ 2024-09-17 230/week @ 2024-09-24 189/week @ 2024-10-01 267/week @ 2024-10-08 910/week @ 2024-10-15 1153/week @ 2024-10-22 842/week @ 2024-10-29 648/week @ 2024-11-05

3,663 downloads per month
Used in 2 crates

MIT license

41KB
630 lines

CRC32

Resurrecting the crc32 crate from the ashes.

Usage

Add crc32-v2 to your Cargo.toml file:

[dependencies]
crc32-v2 = "0.0.4"

or run:

cargo add crc32-v2

Examples

use crc32_v2::crc32;
use crc32_v2::byfour::crc32_little;

const CRC32_INIT: u32 = 0; // Initial CRC value, you can customize it

fn main() {
    // Your data to calculate CRC for
    let data = b"Hello, world!";

    // Calculate CRC
    let result_crc = crc32(CRC32_INIT, data);

    // Print the result
    println!("CRC-32: {:x}", result_crc);

    // Calculate CRC using the little-endian method
    let result_crc_little = crc32_little(CRC32_INIT, data);

    // Print the result
    println!("CRC-32 (Little Endian): {:x}", result_crc_little);
}

// Output

// CRC-32: ebe6c6e6
// CRC-32 (Little Endian): a29eb9bf

lib.rs:

CRC32 V2

This crate provides a simple CRC32 implementation in Rust.

Usage

To use this crate, add the following to your Cargo.toml file:

[dependencies]
crc32_v2 = "0.0.4"

Then, you can use the crc32 or crc32_little functions to calculate the CRC32 checksum of a byte buffer.

Example

use crc32_v2::byfour::crc32_little;
use crc32_v2::crc32;

let crc = crc32(0, &[0u8, 1u8, 2u8, 3u8]);
assert_eq!(crc, 0x8BB98613);
let crc_little = crc32_little(crc, &[0u8, 1u8, 2u8, 3u8]);
assert_eq!(crc, 0x8BB98613);

Implementation Details

The CRC32 algorithm is implemented using a standard polynomial and lookup tables for optimization.

The crc32 function takes two parameters:

  • start_crc: the initial CRC32 value (usually 0)
  • buf: a slice containing the input bytes

It returns a u32, which is the CRC32 checksum of the input buffer.

No runtime deps