Giter Club home page Giter Club logo

crc's Introduction

CRC

Build Status Hex version

This module is used to calculate CRC (Cyclic Redundancy Check) values for binary data. It uses NIF functions written in C to iterate over the given binary calculating the CRC checksum value. The NIFs are written to report their time slice usage and will not interfere with the schedulers.

Installation

Elixir

  1. Add crc to your list of dependencies in mix.exs:
  def deps do
    [{:crc, "~> 0.10"}]
  end

Erlang

  1. add crc to your rebar.config
{deps, [
  {crc, "0.10.0"}
]}.

or erlang.mk

dep_crc = hex 0.10.0

Supported algorithms (models)

Run CRC.list/0 to get a full list of all pre-defined models or CRC.list/1 with a filter to search for a pre-defined model.

Usage

To calculate a CRC-16 X-Modem checksum for the binary <<1,2,3,4,5,4,3,2,1>> using the pre-defined model:

iex> CRC.crc(:crc_16_xmodem, <<1,2,3,4,5,4,3,2,1>>)
31763
iex> CRC.calculate(<<1,2,3,4,5,4,3,2,1>>, :crc_16_xmodem)
31763

Or you can create a model at runtime, this can be done with a map:

iex> CRC.crc(
  %{
    width: 16,
    poly: 0x1021,
    init: 0x00,
    refin: false,
    refout: false,
    xorout: 0x00
  }, 
  <<1,2,3,4,5,4,3,2,1>>
)
31763
iex> CRC.calculate(   
  <<1,2,3,4,5,4,3,2,1>>,
  %{
    width: 16,
    poly: 0x1021,
    init: 0x00,
    refin: false,
    refout: false,
    xorout: 0x00
  }
)
31763

Or you can extend one of the pre-defined models:

iex> CRC.crc(
  %{
    extend: :crc_16_xmodem,
    init: 0x00,
  }, 
  <<1,2,3,4,5,4,3,2,1>>
)
31763

CRC.crc_init/1 is used to create a resource and be used to do partial updates to a calculation that is then finalized later:

iex> resource = CRC.crc_init(:crc_16_xmodem)
#Reference<x.x.x.x>
iex> resource2 = CRC.crc_update(resource, <<1, 2, 3, 4, 5>>)
#Reference<y.y.y.y>
iex> resource3 = CRC.crc_update(resource2, <<4, 3, 2, 1>>)
#Reference<z.z.z.z>
iex>CRC.crc_final(resource3)
31763

This could be usefule to calculate a CRC for a larger binary that you are receiving asyncronously.

Tests

CRC implementations have been tested against these online calculators to validate their correctness to the best of our ability.

There are also two property tests that can use PyCRC or CRC RevEng if installed and configured locally.

$ export PYCRC_BIN=~/pycrc-0.9.1/pycrc.py
$ export REVENG_BIN=~/reveng-1.5.2/reveng
$ mix test

PyCRC is used as a part of the TravisCI test suite.

crc's People

Contributors

tattdcodemonkey avatar fhunleth avatar potatosalad avatar jjcarstens avatar ellbee avatar raphexion avatar niftytushar avatar heavyk avatar

Watchers

James Cloos avatar  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.