crc16 function

Uint8List crc16 (
  1. Uint8List bytes
)

Implementation

Uint8List crc16(Uint8List bytes) {
  // CCITT
  const POLYNOMIAL = 0x1021;
  // XMODEM
  const INIT_VALUE = 0x0000;

  final bitRange = Iterable.generate(8);

  var crc = INIT_VALUE;
  for (var byte in bytes) {
    crc ^= (byte << 8);
    for (var i in bitRange) {
      crc = (crc & 0x8000) != 0 ? (crc << 1) ^ POLYNOMIAL : crc << 1;
    }
  }
  // FIXME setInt16 or setUint16
  var byteData = ByteData(2)..setInt16(0, crc, Endian.little);
  return byteData.buffer.asUint8List();
}