crc16 function

Uint8List crc16(
  1. Uint8List data
)

Implementation

Uint8List crc16(Uint8List data) {
  const int poly = 0x1021;
  int reg = 0;
  final message = Uint8List(data.length + 2);
  message.setAll(0, data);

  for (int byte in message) {
    int mask = 0x80;
    while (mask > 0) {
      reg <<= 1;
      if ((byte & mask) != 0) {
        reg += 1;
      }
      mask >>= 1;

      if (reg > 0xffff) {
        reg &= 0xffff;
        reg ^= poly;
      }
    }
  }

  return Uint8List.fromList([(reg >> 8) & 0xff, reg & 0xff]);
}