# CRC (crc v0.10.4) View Source

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.

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

# Link to this section Summary

## Functions

Calculate a CRC checksum for the `input` based on the crc `params` given.

Calculates a 16-bit CCITT CRC with the given seed, seed defaults to 0xFFFF if one is not given.

Calculates a 16-bit CCITT 0x1D0F CRC

Calculates a 16-bit CCITT Kermit CRC

Calculates a 16-bit CCITT XMODEM CRC

Calculates an XOR checksum for the given binary

Calculate a CRC checksum for the `input` based on the crc `params` given.

Calculates a 16-bit ANSI CRC checksum for the provided binary

Calculates a 16-bit DNP CRC

Calculates a 16-bit modbus CRC

Calculates a 16-bit Sick CRC

Calculates a 32-bit CRC

Calculates a 8-bit CRC with polynomial x^8+x^6+x^3+x^2+1, 0x14D. Chosen based on Koopman, et al. (0xA6 in his notation = 0x14D >> 1): http://www.ece.cmu.edu/~koopman/roses/dsn04/koopman04_crc_poly_embedded.pdf

Takes a `resource` result from `crc_update/2` and finalizes the multi-part CRC calculation.

Initialize a resource to be used for doing CRC calculations. The returned resource can be used with `crc/2` or `crc_update/2` to calculate CRC checksums.

Begins or continues a multi-part CRC calculation.

Returns a list of all the compiled CRC models.

Returns a list of all compiled CRC Models that match the filter given.

# Link to this section Functions

Link to this function

# calculate(input, params)

View Source

## Specs

`calculate(iodata(), :crc_algorithm.params()) :: :crc_algorithm.value()`

Calculate a CRC checksum for the `input` based on the crc `params` given.

See `CRC.crc/2` for details on valid `params`.

This function has the parameter order reversed to allow easier use with pipelines. allowing code to be written like:

## Examples

``read_data() |> CRC.calculate(:crc_16) |> do_something()``
Link to this function

# ccitt_16(input)

View Source

## Specs

`ccitt_16(binary()) :: number()`

Calculates a 16-bit CCITT CRC with the given seed, seed defaults to 0xFFFF if one is not given.

This CCIT method uses a 0x1021 polynomial.

Link to this function

# ccitt_16(input, seed)

View Source

## Specs

`ccitt_16(binary(), number()) :: number()`
Link to this function

# ccitt_16_1D0F(input)

View Source

## Specs

`ccitt_16_1D0F(binary()) :: number()`

Calculates a 16-bit CCITT 0x1D0F CRC

This CCIT method uses a 0x1021 polynomial.

Link to this function

# ccitt_16_kermit(input)

View Source

## Specs

`ccitt_16_kermit(binary()) :: number()`

Calculates a 16-bit CCITT Kermit CRC

This CCIT method uses a 0x8408 polynomial.

Link to this function

# ccitt_16_kermit(input, seed)

View Source

## Specs

`ccitt_16_kermit(binary(), number()) :: number()`
Link to this function

# ccitt_16_xmodem(input)

View Source

## Specs

`ccitt_16_xmodem(binary()) :: number()`

Calculates a 16-bit CCITT XMODEM CRC

This CCIT method uses a 0x1021 polynomial.

Link to this function

# checksum_xor(input)

View Source

## Specs

`checksum_xor(binary()) :: number()`

Calculates an XOR checksum for the given binary

Link to this function

# crc(params, input)

View Source

## Specs

`crc(:crc_algorithm.params(), iodata()) :: :crc_algorithm.value()`

Calculate a CRC checksum for the `input` based on the crc `params` given.

`params` can be an atom for one of the compiled models. See `CRC.list/0` for a full list or a Map with parameters to create a model at runtime. The map given should have all of the following keys:

`width` - (unsigned integer) representation for the width of the CRC in bits `poly` - (unsigned integer) the polynomial used for the CRC calculation `init` - (unsigned integer) The initial value used when starting the calculation `refin` - (boolean) if the input value should be reflected. This is used for changing between endian's `refout` - (boolean) if the outvalue should be reflected when calculation is completed `xorout` - (unsigned integer) Final xor value used when completing the CRC calculation

## Examples

``````%{
width: 16,
poly: 0x1021,
init: 0x00,
refin: false,
refout: false,
xorout: 0x00
}``````

You can also extend one of the compiled models at runtime by creating a map with `extend` key set to the model you wish to extend and the keys you wish to override for that model.

For example to override the initial value for the `:crc_16_ccitt_false` model to `0x1D0F` you would pass the following Map as params:

```%{extend: :crc_16_ccitt_false, init: 0x1D0F}```

Link to this function

# crc_16(input)

View Source

## Specs

`crc_16(binary()) :: number()`

Calculates a 16-bit ANSI CRC checksum for the provided binary

Link to this function

# crc_16_dnp(input)

View Source

## Specs

`crc_16_dnp(binary()) :: number()`

Calculates a 16-bit DNP CRC

Link to this function

# crc_16_modbus(input)

View Source

## Specs

`crc_16_modbus(binary()) :: number()`

Calculates a 16-bit modbus CRC

Link to this function

# crc_16_sick(input)

View Source

## Specs

`crc_16_sick(binary()) :: number()`

Calculates a 16-bit Sick CRC

Link to this function

# crc_32(input)

View Source

## Specs

`crc_32(binary()) :: number()`

Calculates a 32-bit CRC

Link to this function

# crc_8(input, seed \\ 255)

View Source

## Specs

`crc_8(binary(), number()) :: number()`

Calculates a 8-bit CRC with polynomial x^8+x^6+x^3+x^2+1, 0x14D. Chosen based on Koopman, et al. (0xA6 in his notation = 0x14D >> 1): http://www.ece.cmu.edu/~koopman/roses/dsn04/koopman04_crc_poly_embedded.pdf

seed defaults to 0xFF if one is not given

Link to this function

# crc_final(resource)

View Source

## Specs

`crc_final(:crc_algorithm.resource()) :: :crc_algorithm.value()`

Takes a `resource` result from `crc_update/2` and finalizes the multi-part CRC calculation.

Link to this function

# crc_init(params)

View Source

## Specs

`crc_init(:crc_algorithm.params()) :: :crc_algorithm.resource()`

Initialize a resource to be used for doing CRC calculations. The returned resource can be used with `crc/2` or `crc_update/2` to calculate CRC checksums.

Resource is created using the same `params` types that are used with `crc/2`:

• atom's for compiled models
• Map with model values
• Map to extend a compiled model.

If used with `crc/2` the returned resource can be re-used multiple times, but using a map or atom for a compiled model will likely be slightly more performant.

When using with `crc_update/2` a new resource will be returned with every call that should be used to continue the calculation.

Link to this function

# crc_update(resource, input)

View Source

## Specs

`crc_update(:crc_algorithm.resource(), iodata()) :: :crc_algorithm.resource()`

Begins or continues a multi-part CRC calculation.

Takes a `resource` from result of `crc_init/1` or previous `crc_update/2` call, and binary `input`, returns a new `resource` to be used to continue or finalize the CRC calculation.

Link to this function

# list()

View Source

## Specs

`list() :: [{atom(), String.t()}]`

Returns a list of all the compiled CRC models.

Link to this function

# list(filter)

View Source

## Specs

`list(binary()) :: [{atom(), String.t()}]`

Returns a list of all compiled CRC Models that match the filter given.

Filter is compiled into a regular expression and matched against the model name and description.