Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

v0 source pure

Package crc32 implements the CRC-32 checksum as a pure, reusable package.

Readme View source

gno.land/p/moul/x/daily/crc32/v0

CRC-32 checksumChecksum, ChecksumHex, ChecksumWith, Verify, MakeTable, Update, Finalize, Hex, Split.

Treats a message as one enormous binary number and takes the remainder of dividing it by a fixed polynomial. The bit-reflected, table-driven form — the one zip, gzip, PNG and Ethernet actually use.

1import "gno.land/p/moul/x/daily/crc32/v0"
2
3crc32.ChecksumHex("123456789")   // "cbf43926" — the standard check value
4crc32.Checksum("abc")            // 0x352441C2
5crc32.Verify("abc", 0x352441C2)  // true

Three polynomials: IEEE (zip/gzip/PNG, the default), Castagnoli (iSCSI/btrfs, better error detection) and Koopman. MakeTable builds the 256-entry table once — that is the whole point of the table-driven form, trading 1 KiB for eight bit-shifts per byte.

Update/Finalize take and return the raw register, not the finished checksum, so a long message can be fed in chunks. Chaining on final values would be wrong; there is a test asserting chunked equals one-shot.

⚠️ A CRC detects accidents, not tampering. It is linear, so anyone can craft a different message with the same checksum. Never use it to authenticate anything.

Live demo: r/moul/x/daily/crc32demo · render it at /r/moul/x/daily/crc32demo/v0.


Part of moul/gno-contracts — moul's versioned gno.land contracts. See the repository for the full catalog, build/test tooling, and usage.

🧪 Highly experimental — potentially vibe-coded. Not audited; may break, change, or be removed at any time. Do not use with anything of value. Full disclaimer: DISCLAIMER.

Overview

Package crc32 implements the CRC-32 checksum as a pure, reusable package.

CRC-32 treats a message as one enormous binary number and takes the remainder of dividing it by a fixed polynomial. The bit-reflected, table-driven form implemented here is the one everything actually uses — zip, gzip, PNG, and Ethernet all speak IEEE.

Three polynomials are provided:

  • IEEE (0xEDB88320) — zip/gzip/PNG. The default.
  • Castagnoli (0x82F63B78) — iSCSI/btrfs; better error detection.
  • Koopman (0xEB31D82E)

A CRC is an ACCIDENT detector, not a security primitive: it is linear, so anyone can craft a different message with the same checksum. Never use it to authenticate anything.

A live demo of this package is at r/moul/x/daily/crc32demo(/r/moul/x/daily/crc32demo/v0).

Constants 1

Functions 9

func Checksum

1func Checksum(s string) uint32
source

Checksum returns the IEEE CRC-32 of s — the one zip, gzip and PNG use.

func ChecksumHex

1func ChecksumHex(s string) string
source

ChecksumHex is Checksum rendered with Hex.

func ChecksumWith

1func ChecksumWith(s string, t *Table) uint32
source

ChecksumWith returns the CRC-32 of s under the given table.

func Finalize

1func Finalize(crc uint32) uint32
source

Finalize turns a running register into the checksum.

func Hex

1func Hex(crc uint32) string
source

Hex renders a checksum as eight lowercase hex digits, zero-padded, which is how CRCs are conventionally shown.

func Split

1func Split(s string) []string
source

Split returns the checksum of each line of s, in order.

func Update

1func Update(crc uint32, t *Table, s string) uint32
source

Update adds the bytes of s to a running checksum.

Takes and returns the RAW register, not the final value: the caller-visible checksum is the register XOR 0xFFFFFFFF, so chaining Update calls on final values would be wrong. Start from 0 and finish with Finalize.

func Verify

1func Verify(s string, expected uint32) bool
source

Verify reports whether s has the expected checksum.

func MakeTable

1func MakeTable(poly uint32) *Table
source

MakeTable builds the lookup table for a reversed polynomial. Building it once and reusing it is the whole point of the table-driven form: it trades 1 KiB for eight bit-shifts per byte.

Types 1

type Table

array
1type Table [256]uint32
source

Table is a precomputed byte-wise lookup table for one polynomial.

Imports 1

  • strings stdlib

Source Files 3