Git Product home page Git Product logo

go-crc's Introduction

Arbitrary-precision CRC calculator in golang

Can calculate CRCs of any bit width (between CRC-1 and CRC-64) and can process input of any bit length. Automatically creates 256-entry accelerator tables for the used CRC algorithms. Provides presets for and has been tested against the 100+ CRC algorithms listed in Greg Cook's CRC catalogue.

import "github.com/pasztorpisti/go-crc"

func Example() {
    // Using CRC-5/USB to calculate the CRC of a byte slice:
    fmt.Printf("usb1: %#x\n", crc.CRC5USB.Calc([]byte("123456789")))

    // Calculating the CRC when the data arrives in chunks:
    c := crc.CRC5USB.NewCRC()
    c.UpdateBits([]byte("12345"), 8*4+2)
    // The previous call consumed four bytes and the two least significant bits
    // of the last byte. '5' is 0b00110101 in binary so that update would have
    // had the same effect with inputs like "1234\x01" and "1234\xfd".
    // The call below provides the 6 most significant bits of the '5'.
    c.UpdateBits([]byte{0b001101}, 6)
    c.Update([]byte("6789"))
    fmt.Printf("usb2: %#x\n", c.Final())

    // Custom polynomial:
    // 0xa2eb was picked from the CRC Polynomial Zoo:
    // https://users.ece.cmu.edu/~koopman/crc/crc16.html
    a, err := crc.NewAlgo[uint16](16, 0xa2eb, 0xffff, 0xffff, true, true)
    if err != nil {
        panic(err)
    }
    fmt.Printf("zoo/a2eb: %#x\n", a.Calc([]byte("123456789")))

    // Output:
    // usb1: 0x19
    // usb2: 0x19
    // zoo/a2eb: 0x4e4c
}

Here is the godoc that you probably don't need.

go-crc's People

Contributors

pasztorpisti avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.