cassandra_protocol/
crc.rs

1use crc32fast::Hasher;
2
3const CRC24_POLY: i32 = 0x1974f0b;
4const CRC24_INIT: i32 = 0x875060;
5
6/// Computes crc24 value of `bytes`.
7pub fn crc24(bytes: &[u8]) -> i32 {
8    bytes.iter().fold(CRC24_INIT, |mut crc, byte| {
9        crc ^= (*byte as i32) << 16;
10
11        for _ in 0..8 {
12            crc <<= 1;
13            if (crc & 0x1000000) != 0 {
14                crc ^= CRC24_POLY;
15            }
16        }
17
18        crc
19    })
20}
21
22/// Computes crc32 value of `bytes`.
23pub fn crc32(bytes: &[u8]) -> u32 {
24    let mut hasher = Hasher::new();
25    hasher.update(&[0xfa, 0x2d, 0x55, 0xca]); // Cassandra appends a few bytes and forgets to mention it in the spec...
26    hasher.update(bytes);
27    hasher.finalize()
28}