pub(crate) fn crc8(data: &[u8]) -> u8 {
const CRC8_POLYNOMIAL: u8 = 0x31;
let mut crc: u8 = 0xff;
for byte in data {
crc ^= byte;
for _ in 0..8 {
if (crc & 0x80) > 0 {
crc = (crc << 1) ^ CRC8_POLYNOMIAL;
} else {
crc <<= 1;
}
}
}
crc
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn crc8_test_value() {
assert_eq!(crc8(&[0x00]), 0xac);
assert_eq!(crc8(&[0xbe, 0xef]), 0x92);
}
}