crc16

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2021 License: MIT Imports: 1 Imported by: 1

README

crc16

Build Status GoDoc

Go implementation of CRC-16 calculation for majority of widely-used polinomials.

Usage

package main

import (
	"fmt"
	"github.com/getrak/crc16"
)

func main() {
	table := crc16.MakeTable(crc16.CRC16_MAXIM)
	crc := crc16.Checksum([]byte("Hello world!"), table)
	fmt.Printf("CRC-16 MAXIM: %X", crc)
}

Documentation

For more documentation see package documentation

License

The MIT License (MIT)

Copyright (c) 2015 sigurn Copyright (c) 2021 Getrak

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package crc16 implements the 16-bit cyclic redundancy check, or CRC-16, checksum.

It provides parameters for the majority of well-known CRC-16 algorithms.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Alias: ARC, CRC-16, CRC-16/LHA, CRC-IBM
	CRC16_ARC = Params{0x8005, 0x0000, true, true, 0x0000, 0xBB3D, "CRC-16/ARC"}
	// Alias: CRC-16/SPI-FUJITSU
	CRC16_AUG_CCITT = Params{0x1021, 0x1D0F, false, false, 0x0000, 0xE5CC, "CRC-16/AUG-CCITT"}
	// Alias: CRC-16/UMTS, CRC-16/VERIFONE
	CRC16_BUYPASS = Params{0x8005, 0x0000, false, false, 0x0000, 0xFEE8, "CRC-16/BUYPASS"}
	// Alias: CRC-16/IBM-3740, CRC-16/AUTOSAR
	CRC16_CCITT_FALSE = Params{0x1021, 0xFFFF, false, false, 0x0000, 0x29B1, "CRC-16/CCITT-FALSE"}
	CRC16_CDMA2000    = Params{0xC867, 0xFFFF, false, false, 0x0000, 0x4C06, "CRC-16/CDMA2000"}
	CRC16_DDS_110     = Params{0x8005, 0x800D, false, false, 0x0000, 0x9ECF, "CRC-16/DDS-110"}
	// Alias: R-CRC-16
	CRC16_DECT_R = Params{0x0589, 0x0000, false, false, 0x0001, 0x007E, "CRC-16/DECT-R"}
	// Alias: X-CRC-16
	CRC16_DECT_X   = Params{0x0589, 0x0000, false, false, 0x0000, 0x007F, "CRC-16/DECT-X"}
	CRC16_DNP      = Params{0x3D65, 0x0000, true, true, 0xFFFF, 0xEA82, "CRC-16/DNP"}
	CRC16_EN_13757 = Params{0x3D65, 0x0000, false, false, 0xFFFF, 0xC2B7, "CRC-16/EN-13757"}
	// Alias: CRC-16/DARC, CRC-16/EPC, CRC-16/EPC-C1G2, CRC-16/I-CODE
	CRC16_GENIBUS = Params{0x1021, 0xFFFF, false, false, 0xFFFF, 0xD64E, "CRC-16/GENIBUS"}
	// Alias: CRC-16/MAXIM-DOW
	CRC16_MAXIM    = Params{0x8005, 0x0000, true, true, 0xFFFF, 0x44C2, "CRC-16/MAXIM"}
	CRC16_MCRF4XX  = Params{0x1021, 0xFFFF, true, true, 0x0000, 0x6F91, "CRC-16/MCRF4XX"}
	CRC16_RIELLO   = Params{0x1021, 0xB2AA, true, true, 0x0000, 0x63D0, "CRC-16/RIELLO"}
	CRC16_T10_DIF  = Params{0x8BB7, 0x0000, false, false, 0x0000, 0xD0DB, "CRC-16/T10-DIF"}
	CRC16_TELEDISK = Params{0xA097, 0x0000, false, false, 0x0000, 0x0FB3, "CRC-16/TELEDISK"}
	CRC16_TMS37157 = Params{0x1021, 0x89EC, true, true, 0x0000, 0x26B1, "CRC-16/TMS37157"}
	CRC16_USB      = Params{0x8005, 0xFFFF, true, true, 0xFFFF, 0xB4C8, "CRC-16/USB"}
	CRC16_CRC_A    = Params{0x1021, 0xC6C6, true, true, 0x0000, 0xBF05, "CRC-16/CRC-A"}
	// Alias: CRC-16/CCITT, CRC-16/CCITT-TRUE, CRC-16/V-41-LSB, CRC-CCITT, KERMIT
	CRC16_KERMIT = Params{0x1021, 0x0000, true, true, 0x0000, 0x2189, "CRC-16/KERMIT"}
	// Alias: MODBUS
	CRC16_MODBUS = Params{0x8005, 0xFFFF, true, true, 0x0000, 0x4B37, "CRC-16/MODBUS"}
	// Alias: CRC-16/IBM-SDLC, CRC-16/ISO-HDLC, CRC-16/ISO-IEC-14443-3-B, CRC-B, X-25
	CRC16_X_25 = Params{0x1021, 0xFFFF, true, true, 0xFFFF, 0x906E, "CRC-16/X-25"}
	// Alias: CRC-16/ACORN, CRC-16/LTE, CRC-16/V-41-MSB, XMODEM, ZMODEM
	CRC16_XMODEM = Params{0x1021, 0x0000, false, false, 0x0000, 0x31C3, "CRC-16/XMODEM"}
	CRC16_CMS    = Params{0x8005, 0xFFFF, false, false, 0x0000, 0xAEE7, "CRC-16/CMS"}
	CRC16_GSM    = Params{0x1021, 0x0000, false, false, 0xFFFF, 0xCE3C, "CRC-16/GSM"}
	// Alias: CRC-16/ISO-IEC-14443-3-A
	CRC_A              = Params{0x1021, 0xC6C6, true, true, 0x0000, 0xBF05, "CRC-A"}
	CRC16_LJ1200       = Params{0x6F63, 0x0000, false, false, 0x0000, 0xBDF4, "CRC-16/LJ1200"}
	CRC16_NRSC_5       = Params{0x080B, 0xFFFF, true, true, 0x0000, 0xA066, "CRC-16/NRSC-5"}
	CRC16_OPENSAFETY_A = Params{0x5935, 0x0000, false, false, 0x0000, 0x5D38, "CRC-16/OPENSAFETY-A"}
	CRC16_OPENSAFETY_B = Params{0x755b, 0x0000, false, false, 0x0000, 0x20FE, "CRC-16/OPENSAFETY-B"}
	// Alias: CRC-16/IEC-61158-2
	CRC16_PROFIBUS = Params{0x1DCF, 0xFFFF, false, false, 0xFFFF, 0xA819, "CRC-16/PROFIBUS"}
)

Predefined CRC-16 algorithms. List of algorithms with their parameters borrowed from here - http://reveng.sourceforge.net/crc-catalogue/16.htm

The variables can be used to create Table for the selected algorithm.

Functions

func Checksum

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

Checksum returns CRC checksum of data usign scpecified algorithm represented by the Table.

func Complete

func Complete(crc uint16, table *Table) uint16

Complete returns the result of CRC calculation and post-calculation processing of the crc.

func Init

func Init(table *Table) uint16

Init returns the initial value for CRC register corresponding to the specified algorithm.

func Update

func Update(crc uint16, data []byte, table *Table) uint16

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

Types

type Params

type Params struct {
	Poly   uint16
	Init   uint16
	RefIn  bool
	RefOut bool
	XorOut uint16
	Check  uint16
	Name   string
}

Params represents parameters of CRC-16 algorithms. More information about algorithms parametrization and parameter descriptions can be found here - http://www.zlib.net/crc_v3.txt

type Table

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

Table is a 256-word table representing polinomial and algorithm settings for efficient processing.

func MakeTable

func MakeTable(params Params) *Table

MakeTable returns the Table constructed from the specified algorithm.

Jump to

Keyboard shortcuts

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