Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions checksum/CRC-16.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Package checksum provides functions for calculating various checksum values.
package checksum

import "fmt"

// CRC16 computes the CRC-16 checksum of the input data.
func CRC16(data []byte) uint16 {
var crc uint16 = 0xFFFF
for _, b := range data {
crc ^= uint16(b)
for i := 0; i < 8; i++ {
if crc&0x0001 != 0 {
crc = (crc >> 1) ^ 0xA001
} else {
crc >>= 1
}
}
}
return crc
}

// CRC16Hex computes the CRC-16 checksum of a string and returns it as a hexadecimal string.
func CRC16Hex(input string) string {
crc := CRC16([]byte(input))
return fmt.Sprintf("%04X", crc)
}