Rill v0.13 Reference

Standard library · Data formats

data/checksum

Imported as import "data/checksum" as checksum, its names are then checksum.…. Every signature below is the one the checker infers.

The two checksums a file format is likely to ask for: CRC-32 (IEEE 802.3, the one in zip, gzip and PNG) and Adler-32 (zlib's). Neither is a hash for keeping anything secret — crypto/sha2 is for that — but they catch a flipped bit in a page or a frame, and a format that carries one has to be written with the same one.

Both take the bytes of a Str and return an Int in 0..2^32. crc32_from and adler32_from continue a checksum across pieces, so a file can be summed as it is read rather than held whole.

checksum.crc32("123456789")              # => 3421780262
checksum.adler32("Wikipedia")             # => 300286872

Functions

fn crc32(bytes: Str) -> Int

checksum.crc32("")            # => 0
checksum.crc32("123456789")   # => 3421780262

fn crc32_from(sofar: Int, bytes: Str) -> Int

The CRC so far, extended by bytes. crc32_from(0, a + b) and crc32_from(crc32(a), b) are the same number.

checksum.crc32_from(checksum.crc32("1234"), "56789")   # => 3421780262

fn adler32(bytes: Str) -> Int

checksum.adler32("")            # => 1
checksum.adler32("Wikipedia")   # => 300286872

fn adler32_from(sofar: Int, bytes: Str) -> Int

The sum so far, extended by bytes; the two halves are reduced every 5552 bytes, the most that fits before a 32-bit sum would overflow — here the integer is wider, but the reduction is kept where zlib keeps it so the arithmetic is the same arithmetic.

checksum.adler32_from(checksum.adler32("Wiki"), "pedia")   # => 300286872

Tests

  • test_crc32_check_value
  • test_crc32_in_pieces
  • test_adler32_values