use crc32fast::Hasher;
pub fn calculate_crc32(parts: &[&[u8]]) -> u32 {
let mut hasher = Hasher::new();
for part in parts {
hasher.update(part);
}
hasher.finalize()
}
pub fn calculate_crc32_single(data: &[u8]) -> u32 {
let mut hasher = Hasher::new();
hasher.update(data);
hasher.finalize()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_crc32_empty() {
let checksum = calculate_crc32(&[]);
assert_eq!(checksum, 0);
}
#[test]
fn test_crc32_single() {
let data = b"Hello, World!";
let checksum = calculate_crc32_single(data);
assert_ne!(checksum, 0);
}
#[test]
fn test_crc32_multiple_parts() {
let part1 = b"Hello, ";
let part2 = b"World!";
let checksum_combined = calculate_crc32(&[part1, part2]);
let checksum_single = calculate_crc32_single(b"Hello, World!");
assert_eq!(checksum_combined, checksum_single);
}
#[test]
fn test_crc32_deterministic() {
let data = b"test data";
let checksum1 = calculate_crc32_single(data);
let checksum2 = calculate_crc32_single(data);
assert_eq!(checksum1, checksum2);
}
}