Packages

A Native Erlang Configurable CRC function generator

Current section

Files

Jump to
cerlc README.md
Raw

README.md

<!--
  SPDX-FileCopyrightText: 2020 Mark Sebald
  SPDX-FileCopyrightText: 2025 Frank Hunleth
  SPDX-License-Identifier: CC-BY-4.0
-->
# cerlc

## A Custom Configurable Cyclic Redundancy Check (CRC), Function Generator

[![Erlang CI](https://github.com/mdsebald/cerlc/workflows/Erlang%20CI/badge.svg)](https://github.com/mdsebald/cerlc/actions)

* Implemented in native Erlang. No NIFs or dependencies required.
* Select from over 50 predefined CRC algorithms. See src/cerlc.erl for complete list
* OR Generate custom CRC algorithms by manually specifying the parameters bit width, polynomial, initial value, final XOR value, and data reflected or not.

### Build

```sh
rebar3 compile
```

### Test

```sh
rebar3 eunit
```

### Use

#### In an Erlang application

```erlang
% Add to list of dependencies in rebar.config
{deps, [
    {cerlc, "0.2.0"}
]}.

% Example: Using a predefined CRC algorithm
CrcDefn = cerlc:init(crc16_aug_ccitt),

% Data may be a binary or list of bytes
Crc = cerlc:calc_crc(Data, CrcDefn)

% Example: Creating a custom CRC algorithm
% Custom CRC parameters: {Bits, Polynomial, InitValue, FinalXorValue, Reflected}
CustomDefn = cerlc:init({8, 16#4F, 0, 16#FF, false}),

Crc = cerlc:calc_crc(Data, CustomDefn)
```

#### In an Elixir application

```elixir
# Add to list of dependencies in mix.exs
def deps do
  [
    {:cerlc, "~> 0.2.0"}
  ]
end


# Example: Using a predefined CRC algorithm
crc8_defn = :cerlc.init(:crc8)

# Data may be a binary or list of bytes
crc = :cerlc.calc_crc(data, crc8_defn)

# Example: Creating a custom CRC algorithm
# Custom CRC parameters: {bits, polynomial, init_value, final_xor_value, reflected}
custom_defn = :cerlc.init({16, 0x1234, 0xFFFF, 0xFFFF, true}),

crc = :cerlc.calc_crc(data, custom_defn)

# Example: Using a module attribute to evaluate init() function at compile time
@crc32_defn :cerlc.init(:crc32_c)

crc = :cerlc.calc_crc(data, @crc32_defn)
```

### Performance

cerlc is slower than Erlang's built in crc32() function and CRC's implemented using 'C' language NIF's.

cerlc is useful for quickly generating CRC's with little code and no other dependencies.

### Docs

Generate HTML docs locally with:
```sh
rebar3 ex_doc
```
The output is written to `_build/default/lib/cerlc/doc/index.html`.

### Sources of CRC Definitions

* [Javascript CRC Calculator](http://www.sunshine2k.de/coding/javascript/crc/crc_js.html)
* [crccalc](https://crccalc.com/)