Skip to content
Jennifer Programming Language

crc - cyclic redundancy checks

Enable with use crc;. Computes CRC-32 (IEEE polynomial) and CRC-64/XZ checksums over bytes, with the same shape as hash. Output is the natural-width digest in big-endian byte order.

CRCs catch transport / storage corruption; they do not resist deliberate tampering. For content addressing or signatures use hash ("sha256" or wider). The separate library keeps the difference visible at the import line.

jennifer
use encoding;
use io;
use crc;

def input as bytes init encoding.encode("utf-8", "abc");
def sum as bytes init crc.compute("crc32", $input);
io.printf("crc32 checksum is %d bytes\n", len($sum));

Algorithm selection

Algo stringOutput widthPolynomial
"crc32"4 bytesIEEE 802.3 (CRC-32/ISO-HDLC).
"crc64"8 bytesCRC-64/XZ (Go crc64.ECMA table).

"crc64" is CRC-64/XZ (also called CRC-64/GO-ECMA): ECMA-182 polynomial, reflected, init and xorout all ones; check value 995dc9bbdf1939fa for "123456789". CRC-64/ECMA-182 (6c40df5f0b497347) is a different parameterisation and is not provided.

Output bytes are big-endian (network byte order), matching the display of Sum() from Go's CRC types.

Passing an unknown algorithm is a positioned runtime error: crc.compute: unknown algorithm "adler32"; known: "crc32", "crc64".

One-shot

CallReturnsNotes
crc.compute(algo, b)bytesFull checksum of the entire input. Big-endian.

Streaming

Feed chunks into a stream handle and finalize. Same shape as hash:

jennifer
use crc;
def s as crc.Stream init crc.stream("crc32");
crc.update($s, $chunkOne);
crc.update($s, $chunkTwo);
def sum as bytes init crc.finalize($s);
CallReturnsNotes
crc.stream(algo)crc.StreamAllocate a fresh handle for the named algorithm.
crc.update($s, $bytes)nullFeed one chunk. Mutates the handle's state by side effect.
crc.finalize($s)bytesCompute the checksum and consume the handle. A later update / finalize throws kind "closed".
crc.discard($s)nullDrop a stream without computing its checksum, releasing its state. On a finalized or discarded stream it does nothing, so defer crc.discard($s); is safe.

At most 4096 streams are open at once; each needs a matching finalize or discard.

Errors

All errors are positioned at the call site.

CauseExample message
Wrong aritycrc.compute expects 2 arguments (algo, bytes), got 1
Wrong argument typecrc.compute: second argument must be bytes, got string
Unknown algorithmcrc.compute: unknown algorithm "adler32"; known: "crc32", "crc64"
Reuse of a finalized or discarded stream (kind "closed")crc.update: stream 3 has already been finalized or discarded, or never existed
Leaked streams (kind "limit")crc.stream: too many open streams (limit 4096); each needs a matching crc.finalize or discard

See also

  • hash.md - cryptographic-style digests (MD5, SHA-1, SHA-2).
  • encoding.md - hex / base64 rendering of a checksum.

Academy talks

TalkWhat it shows
A terminal game in Jennifera CRC-32 guarding the high score file