crc16

package
v5.1.42 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2024 License: Apache-2.0, BSD-2-Clause Imports: 2 Imported by: 0

README

crc16 GoDoc

Package crc16 implements the 16-bit Cyclic Redundancy Check

Download:

go get github.com/npat-efault/crc16

Package crc16 is a Golang implementation of the 16-bit Cyclic Redundancy Check, or CRC-16 checksum.

The package's API is almost identical to the standard-library's hash/crc32 and hash/crc64 packages.

Package crc16 supports CRC calculation in all possible configurations: Polynomial, bit-order, byte-order, initial value, and final value can all be selected. Predefined configurations are supplied for the most common uses (e.g. PPP, X25, Modbus, etc).

(See also)

Documentation

Overview

Package crc16 implements the 16-bit cyclic redundancy check, or CRC-16, checksum. See http://en.wikipedia.org/wiki/Cyclic_redundancy_check for information.

Index

Constants

View Source
const Size = 2

The size of a CRC-16 checksum in bytes.

Variables

View Source
var (
	X25 = &Conf{
		Poly: 0x1021, BitRev: true,
		IniVal: 0xffff, FinVal: 0xffff,
		BigEnd: false,
	}
	PPP    = X25
	Modbus = &Conf{
		Poly: 0x8005, BitRev: true,
		IniVal: 0xffff, FinVal: 0x0,
		BigEnd: false,
	}
	XModem = &Conf{
		Poly: 0x1021, BitRev: false,
		IniVal: 0x0000, FinVal: 0x0,
		BigEnd: true,
	}
	Kermit = &Conf{
		Poly: 0x1021, BitRev: true,
		IniVal: 0x0, FinVal: 0x0,
		BigEnd: false,
	}
)

Typical CRC-16 configurations. Mostly used are the CCITT (0x1021) and the IBM/ANSI (0x8005) polynomials, either bit-reversed or not. For more configurations see: http://reveng.sourceforge.net/crc-catalogue/

Functions

func Checksum

func Checksum(c *Conf, data []byte) uint16

Checksum returns the CRC-16 checksum of data using the configuration c.

func Update

func Update(crc uint16, tab *Table, p []byte) uint16

Update returns the CRC-16 checksum of p using the polynomial table tab constructed by MakeTable (bit-reversed order). The resulting CRC is in bit-reversed order (bit-15 corresponds to the X^0 term). Argument crc is the initial value of the CRC register.

func UpdateNBR

func UpdateNBR(crc uint16, tab *Table, p []byte) uint16

UpdateNBR returns the CRC-16 checksum of p using the polynomial table tab constructed by MakeTableNBR (non-bit-reversed order). The resulting CRC is in non-bit-reversed order (bit-0 corresponds to the X^0 term). Argument CRC is the initial value of the CRC register.

Types

type Conf

type Conf struct {
	Poly   uint16 // Polynomial to use.
	BitRev bool   // Bit reversed CRC (bit-15 is X^0)?
	IniVal uint16 // Initial value of CRC register.
	FinVal uint16 // XOR CRC with this at the end.
	BigEnd bool   // Emit *bytes* most significant first (see Hash.Sum)?
	// contains filtered or unexported fields
}

Conf is a CRC configuration. It is passed to functions New and Checksum and specifies the parameters of the calculated checksum. The first time New or Checksum are called with a configuration structure c, they calculate the polynomial table for this configuration; subsequent calls with the same c use the table already calculated. A few commonly used configurations are defined as global variables (X25, PPP, Modbus, etc.)

type Hash16

type Hash16 interface {
	hash.Hash
	Sum16() uint16
}

Hash16 is the common interface implemented by all 16-bit hash functions.

func New

func New(c *Conf) Hash16

New creates a new hash.Hash16 computing the CRC-16 checksum using the configuration c.

type Table

type Table [256]uint16

Table is a 256-word table representing the polynomial for efficient processing.

func MakeTable

func MakeTable(poly uint16) *Table

MakeTable returns the Table constructed from the specified polynomial. The table is calcuated in bit-reversed order (bit-15 corresponds to the X^0 term). Argument poly must be given bit-reversed (e.g. 0xA001 for the 0x8005 polynomial).

func MakeTableNBR

func MakeTableNBR(poly uint16) *Table

MakeTableNBR returns the Table constructed from the specified polynomial. The table is calculated in non-bit-reversed order (bit-0 corresponds to the X^0 term).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL