├── .travis.yml ├── LICENSE.md ├── README.md ├── crc8.go ├── crc8_test.go ├── go.mod └── go.sum /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | env: 3 | - secure: "BDYvndnBoXOkJ+6hlKAgrhoxKIJPyHJa2c7mz8/WP4Ete+Wc8tj6DN47la8pDe0gijEs4bXGLGALC0ItJZtHSPdN+uDM9rFG2A1UWn+Yn0q6IW6NoZgy6RXw2EYfLmCws4r3JLYxRavK1ZeXzJ382BGJtQKM90s940f02cGJ+eL7lSwiYCyOT7dsq/EVnLRAKyxG7OpDX/F8IOKrX5yZKNPnDwokoD1XfMCo5I6niJnR7P+7FDQ5a63/RR7/cf5uvhf58sl6fujDjmLN5O5AQKL+tUIpWbD+YJqawukgTNxyyaTOxoMXpg3GYhHlZThsUZUFG25ad6l6vKR3ts5XlssDnkC5NJcFDQ8OO9G2ucdr3zm7etXGX37enZcitCfC0p2aHsICkpLnPrkWShAIEp7akjdval7McS4wtG7DderL4IyYn4iMmOkqHTgyjEU02Jm7GhU6GWviEmvvAN8iPyOYtBfNOKPQBFnKSfNvEZWS/A6Y7dQFQvhFbMfu5IBymzz1/pcZfvFruakgOLY94D9VY/0R0CBZoiUqOy2/vzR3EemI/kMxYWrYafiyhWmdv2hgxUCMclrhpzqYPWdT4XZVse0C3iFBYWevL47pEurqX5lLKVrSvf3jZFqL+dgxiVY7edCh7iec7nFGaaTgkD4bjctKNPysxV4LqbMo8YE=" 4 | install: 5 | - go get -d -v ./... 6 | - go get golang.org/x/tools/cmd/cover 7 | - go get github.com/mattn/goveralls 8 | - if ! go get github.com/golang/tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi 9 | script: 10 | - go test -v -covermode=count -coverprofile=coverage.out 11 | - $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN 12 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 sigurn 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # crc8 3 | [![Build Status](https://travis-ci.org/sigurn/crc8.svg?branch=master)](https://travis-ci.org/sigurn/crc8) 4 | [![Coverage Status](https://coveralls.io/repos/sigurn/crc8/badge.svg?branch=master&service=github)](https://coveralls.io/github/sigurn/crc8?branch=master) 5 | [![GoDoc](https://godoc.org/github.com/sigurn/crc8?status.svg)](https://godoc.org/github.com/sigurn/crc8) 6 | 7 | Go implementation of CRC-8 calculation for majority of widely-used polinomials. 8 | 9 | ## Usage 10 | ```go 11 | package main 12 | 13 | import ( 14 | "fmt" 15 | "github.com/sigurn/crc8" 16 | ) 17 | 18 | func main() { 19 | table := crc8.MakeTable(crc8.CRC8_MAXIM) 20 | crc := crc8.Checksum([]byte("Hello world!"), table) 21 | fmt.Printf("CRC-8 MAXIM: %X", crc) 22 | } 23 | ``` 24 | 25 | ## Documentation 26 | For more documentation see [package documentation](https://godoc.org/github.com/sigurn/crc8) 27 | 28 | ## License 29 | 30 | The MIT License (MIT) 31 | 32 | Copyright (c) 2015 sigurn 33 | 34 | 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: 35 | 36 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 37 | 38 | 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. 39 | 40 | 41 | -------------------------------------------------------------------------------- /crc8.go: -------------------------------------------------------------------------------- 1 | // Package crc8 implements the 8-bit cyclic redundancy check, or CRC-8, checksum. 2 | // 3 | // It provides parameters for the majority of well-known CRC-8 algorithms. 4 | package crc8 5 | 6 | import "math/bits" 7 | 8 | // Params represents parameters of a CRC-8 algorithm including polynomial and initial value. 9 | // More information about algorithms parametrization and parameter descriptions 10 | // can be found here - http://www.zlib.net/crc_v3.txt 11 | type Params struct { 12 | Poly uint8 13 | Init uint8 14 | RefIn bool 15 | RefOut bool 16 | XorOut uint8 17 | Check uint8 18 | Name string 19 | } 20 | 21 | // Predefined CRC-8 algorithms. 22 | // List of algorithms with their parameters borrowed from here - http://reveng.sourceforge.net/crc-catalogue/1-15.htm#crc.cat-bits.8 23 | // 24 | // The variables can be used to create Table for the selected algorithm. 25 | var ( 26 | CRC8 = Params{0x07, 0x00, false, false, 0x00, 0xF4, "CRC-8"} 27 | CRC8_CDMA2000 = Params{0x9B, 0xFF, false, false, 0x00, 0xDA, "CRC-8/CDMA2000"} 28 | CRC8_DARC = Params{0x39, 0x00, true, true, 0x00, 0x15, "CRC-8/DARC"} 29 | CRC8_DVB_S2 = Params{0xD5, 0x00, false, false, 0x00, 0xBC, "CRC-8/DVB-S2"} 30 | CRC8_EBU = Params{0x1D, 0xFF, true, true, 0x00, 0x97, "CRC-8/EBU"} 31 | CRC8_I_CODE = Params{0x1D, 0xFD, false, false, 0x00, 0x7E, "CRC-8/I-CODE"} 32 | CRC8_ITU = Params{0x07, 0x00, false, false, 0x55, 0xA1, "CRC-8/ITU"} 33 | CRC8_MAXIM = Params{0x31, 0x00, true, true, 0x00, 0xA1, "CRC-8/MAXIM"} 34 | CRC8_ROHC = Params{0x07, 0xFF, true, true, 0x00, 0xD0, "CRC-8/ROHC"} 35 | CRC8_WCDMA = Params{0x9B, 0x00, true, true, 0x00, 0x25, "CRC-8/WCDMA"} 36 | ) 37 | 38 | // Table is a 256-byte table representing polynomial and algorithm settings for efficient processing. 39 | type Table struct { 40 | params Params 41 | data [256]uint8 42 | } 43 | 44 | // MakeTable returns the Table constructed from the specified algorithm. 45 | func MakeTable(params Params) *Table { 46 | table := new(Table) 47 | table.params = params 48 | for n := 0; n < 256; n++ { 49 | crc := uint8(n) 50 | for i := 0; i < 8; i++ { 51 | bit := (crc & 0x80) != 0 52 | crc <<= 1 53 | if bit { 54 | crc ^= params.Poly 55 | } 56 | } 57 | table.data[n] = crc 58 | } 59 | return table 60 | } 61 | 62 | // Init returns the initial value for CRC register corresponding to the specified algorithm. 63 | func Init(table *Table) uint8 { 64 | return table.params.Init 65 | } 66 | 67 | // Update returns the result of adding the bytes in data to the crc. 68 | func Update(crc uint8, data []byte, table *Table) uint8 { 69 | if table.params.RefIn { 70 | for _, d := range data { 71 | d = bits.Reverse8(d) 72 | crc = table.data[crc^d] 73 | } 74 | } else { 75 | for _, d := range data { 76 | crc = table.data[crc^d] 77 | } 78 | } 79 | return crc 80 | } 81 | 82 | // Complete returns the result of CRC calculation and post-calculation processing of the crc. 83 | func Complete(crc uint8, table *Table) uint8 { 84 | if table.params.RefOut { 85 | crc = bits.Reverse8(crc) 86 | } 87 | 88 | return crc ^ table.params.XorOut 89 | } 90 | 91 | // Checksum returns CRC checksum of data using specified algorithm represented by the Table. 92 | func Checksum(data []byte, table *Table) uint8 { 93 | crc := Init(table) 94 | crc = Update(crc, data, table) 95 | return Complete(crc, table) 96 | } 97 | -------------------------------------------------------------------------------- /crc8_test.go: -------------------------------------------------------------------------------- 1 | package crc8 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | var testData = []byte("123456789") 8 | 9 | func testSelectedCRC8(params Params, t *testing.T) { 10 | table := MakeTable(params) 11 | if table == nil { 12 | t.Errorf("Failed to make %q table", params.Name) 13 | } 14 | 15 | crc := Checksum(testData, table) 16 | 17 | if crc != table.params.Check { 18 | t.Errorf("Invalid %q sample calculation, expected: %X, actual: %X", table.params.Name, table.params.Check, crc) 19 | } 20 | } 21 | 22 | func TestCRC8(t *testing.T) { 23 | testSelectedCRC8(CRC8, t) 24 | } 25 | 26 | func BenchmarkCRC8(b *testing.B) { 27 | table := MakeTable(CRC8) 28 | 29 | for i := 0; i < b.N; i++ { 30 | Checksum(testData, table) 31 | } 32 | } 33 | 34 | func BenchmarkCRC8WCDMA(b *testing.B) { 35 | table := MakeTable(CRC8_WCDMA) 36 | 37 | for i := 0; i < b.N; i++ { 38 | Checksum(testData, table) 39 | } 40 | } 41 | 42 | func TestCRC8_CDMA2000(t *testing.T) { 43 | testSelectedCRC8(CRC8_CDMA2000, t) 44 | } 45 | 46 | func TestCRC8_DARC(t *testing.T) { 47 | testSelectedCRC8(CRC8_DARC, t) 48 | } 49 | 50 | func TestCRC8_DVB_S2(t *testing.T) { 51 | testSelectedCRC8(CRC8_DVB_S2, t) 52 | } 53 | 54 | func TestCRC8_EBU(t *testing.T) { 55 | testSelectedCRC8(CRC8_EBU, t) 56 | } 57 | 58 | func TestCRC8_I_CODE(t *testing.T) { 59 | testSelectedCRC8(CRC8_I_CODE, t) 60 | } 61 | 62 | func TestCRC8_ITU(t *testing.T) { 63 | testSelectedCRC8(CRC8_ITU, t) 64 | } 65 | 66 | func TestCRC8_MAXIM(t *testing.T) { 67 | testSelectedCRC8(CRC8_MAXIM, t) 68 | } 69 | 70 | func TestCRC8_ROHC(t *testing.T) { 71 | testSelectedCRC8(CRC8_ROHC, t) 72 | } 73 | 74 | func TestCRC8_WCDMA(t *testing.T) { 75 | testSelectedCRC8(CRC8_WCDMA, t) 76 | } 77 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/sigurn/crc8 2 | 3 | go 1.17 4 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sigurn/crc8/2243fe600f9fc86b817de9dd42e2bc92417cf354/go.sum --------------------------------------------------------------------------------