crc16

package module
v0.0.0-...-2b2a61e Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2017 License: BSD-3-Clause Imports: 2 Imported by: 61

README

GoDoc Build Status

CRC16

A Go package implementing the 16-bit Cyclic Redundancy Check, or CRC-16, checksum.

Usage

To generate the hash of a byte slice, use the crc16.Checksum() function:

import "github.com/howeyc/crc16"

data := byte("test")
checksum := crc16.Checksum(data, crc16.IBMTable)

The package provides the following hashing tables. For each of these tables, a shorthand can be used.

// This is the same as crc16.Checksum(data, crc16.IBMTable)
checksum := crc16.ChecksumIBM(data)

Using the hash.Hash interface also works.

h := crc16.New(crc16.IBMTable)
data := byte("test")
data2 := byte("data")
h.Write(data)
h.Write(data2)
checksum := h.Sum(nil)

Changelog

  • 2017.03.27 - Added MBus checksum
  • 2017.05.27 - Added checksum function without XOR
  • 2017.12.08 - Implement encoding.BinaryMarshaler and encoding.BinaryUnmarshaler to allow saving and recreating their internal state.

Documentation

Index

Constants

View Source
const (
	// IBM is used by Bisync, Modbus, USB, ANSI X3.28, SIA DC-07, ...
	IBM = 0xA001

	// CCITT is used by X.25, V.41, HDLC FCS, XMODEM, Bluetooth, PACTOR, SD, ...
	// CCITT forward is 0x8408. Reverse is 0x1021.
	CCITT      = 0x8408
	CCITTFalse = 0x1021

	// SCSI is used by SCSI
	SCSI = 0xEDD1

	// MBUS is used by Meter-Bus, DNP, ...
	MBUS = 0x3D65
)

Predefined polynomials.

Variables

View Source
var CCITTFalseTable = makeBitsReversedTable(CCITTFalse)

CCITTFalseTable is the table for CCITT-FALSE.

View Source
var CCITTTable = makeTable(CCITT)

CCITTTable is the table for the CCITT polynomial.

View Source
var IBMTable = makeTable(IBM)

IBMTable is the table for the IBM polynomial.

View Source
var MBusTable = makeBitsReversedTable(MBUS)

MBusTable is the tabe used for Meter-Bus polynomial.

View Source
var SCSITable = makeTable(SCSI)

SCSITable is the table for the SCSI polynomial.

Functions

func Checksum

func Checksum(data []byte, tab *Table) uint16

Checksum returns the CRC-16 checksum of data using the polynomial represented by the Table.

func ChecksumCCITT

func ChecksumCCITT(data []byte) uint16

ChecksumCCITT returns the CRC-16 checksum of data using the CCITT polynomial.

func ChecksumCCITTFalse

func ChecksumCCITTFalse(data []byte) uint16

ChecksumCCITTFalse returns the CRC-16 checksum using what some call the CCITT-False polynomial, which matches what is used by Perl Digest/CRC and Boost for example.

func ChecksumIBM

func ChecksumIBM(data []byte) uint16

ChecksumIBM returns the CRC-16 checksum of data using the IBM polynomial.

func ChecksumMBus

func ChecksumMBus(data []byte) uint16

ChecksumMBus returns the CRC-16 checksum of data using the MBus polynomial.

func ChecksumSCSI

func ChecksumSCSI(data []byte) uint16

ChecksumSCSI returns the CRC-16 checksum of data using the SCSI polynomial.

func Update

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

Update returns the result of adding the bytes in p to the crc.

Types

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(tab *Table) Hash16

New creates a new Hash16 computing the CRC-16 checksum using the polynomial represented by the Table.

func NewCCITT

func NewCCITT() Hash16

NewCCITT creates a new hash.Hash16 computing the CRC-16 checksum using the CCITT polynomial.

func NewIBM

func NewIBM() Hash16

NewIBM creates a new Hash16 computing the CRC-16 checksum using the IBM polynomial.

func NewSCSI

func NewSCSI() Hash16

NewSCSI creates a new Hash16 computing the CRC-16 checksum using the SCSI polynomial.

type Table

type Table struct {
	// contains filtered or unexported fields
}

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

func MakeBitsReversedTable

func MakeBitsReversedTable(poly uint16) *Table

MakeBitsReversedTable returns the Table constructed from the specified polynomial.

func MakeTable

func MakeTable(poly uint16) *Table

MakeTable returns the Table constructed from the specified polynomial.

func MakeTableNoXOR

func MakeTableNoXOR(poly uint16) *Table

MakeTableNoXOR returns the Table constructed from the specified polynomial. Updates happen without XOR in and XOR out.

Jump to

Keyboard shortcuts

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