packkit/checksum

Adler-32 and CRC-32 checksums used by zlib (RFC 1950) and gzip (RFC 1952).

The implementations are pure Gleam and behave identically on the Erlang and JavaScript targets.

Types

Opaque incremental SHA-256 state. Built via sha256_init, extended by sha256_update, finished by sha256_finalize. Useful when the input is produced one small chunk at a time — e.g. the 7z key-derivation function loops 2^numCyclesPower rounds (typically 524288) each appending salt + password + an 8-byte counter; building one giant BitArray to pass to sha256 would be quadratic on the JS target.

pub opaque type Sha256State

Values

pub fn adler32(data data: BitArray) -> Int

Compute the Adler-32 checksum of data, returning the canonical 32-bit value (s2 << 16) | s1.

pub fn adler32_continue(
  previous previous: Int,
  data data: BitArray,
) -> Int

Continue an Adler-32 computation from a previous checksum value.

pub fn bzip2_crc32(data data: BitArray) -> Int

Compute the bzip2 CRC-32 of data.

bzip2 reuses the IEEE 802.3 polynomial 0x04C11DB7 but processes each byte MSB-first (without reflection) and XORs the final value with 0xFFFFFFFF, matching the per-block and stream CRC fields that appear in .bz2 files.

pub fn crc32(data data: BitArray) -> Int

Compute the CRC-32 checksum of data using the IEEE 802.3 reflected polynomial 0xEDB88320 and the same final XOR as zlib’s crc32.

pub fn crc32_continue(
  previous previous: Int,
  data data: BitArray,
) -> Int

Continue a CRC-32 computation from a previous checksum value.

pub fn crc32c(data data: BitArray) -> Int

Compute the CRC-32C (Castagnoli) checksum of data using the reflected polynomial 0x82F63B78. CRC-32C underlies the masked checksums in Snappy’s frame format.

pub fn crc64_xz(data data: BitArray) -> #(Int, Int)

Compute the CRC-64 (ECMA-182, reflected) checksum of data used by the xz block-check field (check_type = 4, see RFC xz-format §2.1.1.2). Polynomial 0x42F0E1EBA9EA3693 reflected to 0xC96C5795D7870F42, init / final-xor 0xFFFFFFFFFFFFFFFF.

The result is returned as a #(low_u32, high_u32) pair instead of a single 64-bit Int so the implementation stays exact on the JavaScript target, whose Number cannot safely represent the full 64-bit space and whose bitwise operators are 32-bit anyway.

pub fn hmac_sha1(
  key key: BitArray,
  data data: BitArray,
) -> BitArray

Compute the HMAC-SHA1 of data keyed by key (RFC 2104). Returns the 20-byte tag as a BitArray.

pub fn pbkdf2_hmac_sha1(
  password password: BitArray,
  salt salt: BitArray,
  iterations iterations: Int,
  dk_len dk_len: Int,
) -> BitArray

Derive dk_len bytes from password and salt via PBKDF2-HMAC-SHA1 (RFC 2898). Used by the ZIP AE-x scheme with iterations = 1000 and dk_len = key_len * 2 + 2 (encryption key + HMAC key + 2-byte password verifier).

pub fn sha1(data data: BitArray) -> BitArray

Compute the SHA-1 digest of data (FIPS 180-4 §6.1). Returns a 20-byte BitArray. Used by hmac_sha1 for ZIP AE-x.

pub fn sha256(data data: BitArray) -> BitArray

Compute the SHA-256 digest of data and return the 32-byte digest as a BitArray. Used by the xz block-check field (check_type = 10).

pub fn sha256_finalize(state state: Sha256State) -> BitArray

Apply FIPS 180-4 padding to whatever bytes are still buffered in state, process the final block(s), and return the 32-byte digest.

pub fn sha256_init() -> Sha256State

Fresh SHA-256 state using the FIPS 180-4 §5.3.3 initial hash constants.

pub fn sha256_update(
  state: Sha256State,
  data data: BitArray,
) -> Sha256State

Absorb data into the running SHA-256 state. Bytes accumulate in an internal buffer and full 64-byte blocks are consumed as soon as they’re available, so callers may pass arbitrarily small chunks without blowing memory.

pub fn snappy_mask(crc crc: Int) -> Int

Mask a CRC-32C value as Snappy’s framing layer requires.

The masking is ((crc >> 15) | (crc << 17)) + 0xa282ead8 taken modulo 2^32.

Search Document