package utils import ( "encoding/binary" ) const WCPOLY uint16 = 0x8408 type CRC16CCITT struct { wCRCin uint16 } func NewCRC16CCITT() *CRC16CCITT { return &CRC16CCITT{} } func (c *CRC16CCITT) Update(b byte) { c.wCRCin ^= uint16(b) for j := 0; j < 8; j++ { if (c.wCRCin & 0x0001) != 0 { c.wCRCin >>= 1 c.wCRCin ^= WCPOLY } else { c.wCRCin >>= 1 } } } func (c *CRC16CCITT) GetWCRCin(rawBytes []byte) []byte { for _, b := range rawBytes { c.Update(b) } //log.Printf("CRC16CCITT=0x%x", c.wCRCin) return uint16toBytes(c.wCRCin) } func uint16toBytes(v uint16) []byte { littleEndianBytes := make([]byte, 2) binary.BigEndian.PutUint16(littleEndianBytes, v) return littleEndianBytes }