kacrab_protocol/crc.rs
1//! CRC32C checksum compute + validation.
2//!
3//! Kafka record batches use CRC32C (Castagnoli polynomial), not CRC32. The
4//! checksum covers the bytes from `attributes` through the end of the batch.
5
6pub mod error;
7
8pub use self::error::CrcMismatch;
9
10/// Compute the CRC32C checksum of `bytes`.
11#[must_use]
12pub fn crc32c(bytes: &[u8]) -> u32 {
13 ::crc32c::crc32c(bytes)
14}
15
16/// Validate that `crc32c(bytes) == expected`. Returns [`CrcMismatch`] otherwise.
17pub fn validate_crc32c(bytes: &[u8], expected: u32) -> Result<(), CrcMismatch> {
18 let actual = crc32c(bytes);
19 if actual == expected {
20 Ok(())
21 } else {
22 Err(CrcMismatch { expected, actual })
23 }
24}