Skip to content

Instantly share code, notes, and snippets.

@archshift
Created September 16, 2017 11:27
Show Gist options
  • Save archshift/080adf97d852d12167531ac1a21716ad to your computer and use it in GitHub Desktop.
Save archshift/080adf97d852d12167531ac1a21716ad to your computer and use it in GitHub Desktop.
fn make_crc(buf: &[u8]) -> u8 {
let crc_for_byte = |mut byte: u8| {
const polynomial: u8 = 0x89;
if byte & 0x80 != 0 {
byte ^= polynomial
}
for _ in 1..8 {
byte <<= 1;
if byte & 0x80 != 0 {
byte ^= polynomial;
}
}
byte
};
buf.iter().fold(0u8, |out_byte, b| crc_for_byte((out_byte << 1) ^ b))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment