Documentation, v0.1.1
This commit is contained in:
parent
c0f80d1177
commit
a670238712
8
crc.go
8
crc.go
@ -1,13 +1,19 @@
|
|||||||
|
// CRC16 implements the CRC-16-CCITT algorithm.
|
||||||
|
// The polynomial is 0x1021.
|
||||||
|
// It accepts a byte slice and returns a 16-bit checksum.
|
||||||
package crc16
|
package crc16
|
||||||
|
|
||||||
var Crc16Table = make([]uint16, 256)
|
var Crc16Table = make([]uint16, 256)
|
||||||
|
|
||||||
|
// GeneratorPolynomial is the polynomial used in the CRC-16-CCITT algorithm.
|
||||||
const GeneratorPolynomial = 0x1021
|
const GeneratorPolynomial = 0x1021
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
calculateTable()
|
calculateTable()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// calculateTable computes the CRC-16 lookup table.
|
||||||
|
// The table is used to speed up the CRC calculation.
|
||||||
func calculateTable() {
|
func calculateTable() {
|
||||||
for dividend := 0; dividend < 256; dividend++ {
|
for dividend := 0; dividend < 256; dividend++ {
|
||||||
crc := uint16(dividend << 8)
|
crc := uint16(dividend << 8)
|
||||||
@ -22,6 +28,8 @@ func calculateTable() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calculates generates a CRC-16 checksum for the given data.
|
||||||
|
// It accepts a byte slice and returns a 16-bit checksum.
|
||||||
func Calculate(data []byte) uint16 {
|
func Calculate(data []byte) uint16 {
|
||||||
crc := uint16(0)
|
crc := uint16(0)
|
||||||
for _, b := range data {
|
for _, b := range data {
|
||||||
|
Loading…
Reference in New Issue
Block a user