---
title: "Checksums"
description: "CRC-32 and Adler-32 and CRC-64 and XXH32 and XXH64. Every one checked on the way out and a wrong one is an error"
canonical_url: "https://compressions.agntn.dev/guide/checksums"
---
# Checksums

> CRC-32 and Adler-32 and CRC-64 and XXH32 and XXH64. Every one checked on the way out and a wrong one is an error

## Checked, not trusted

Every container that stores a checksum gets it checked on the way out. A wrong one is a `ChecksumError`, never a shrug and a warning:

```ts
decompress("deflate", flipped, { container: "gzip" });
// ChecksumError: deflate: gzip CRC-32 does not match at byte 24
```

The bytes did come out, though. They're on the error as `partial`, so a puzzle with a deliberately broken trailer can still be read. [Limits and partial reads](https://compressions.agntn.dev/guide/limits) has the `partial` option that returns them instead of throwing.

## Which one where

<table>
<thead>
  <tr>
    <th>
      Container
    </th>
    
    <th>
      Checksum
    </th>
    
    <th>
      Over
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      zlib
    </td>
    
    <td>
      Adler-32
    </td>
    
    <td>
      the output
    </td>
  </tr>
  
  <tr>
    <td>
      gzip
    </td>
    
    <td>
      CRC-32, and the size modulo 2³²
    </td>
    
    <td>
      the output of each member
    </td>
  </tr>
  
  <tr>
    <td>
      bzip2
    </td>
    
    <td>
      CRC-32/BZIP2
    </td>
    
    <td>
      each block's output, then all blocks combined
    </td>
  </tr>
  
  <tr>
    <td>
      xz
    </td>
    
    <td>
      CRC-32, CRC-64 or SHA-256, as the stream says
    </td>
    
    <td>
      each block's output
    </td>
  </tr>
  
  <tr>
    <td>
      xz
    </td>
    
    <td>
      CRC-32
    </td>
    
    <td>
      the stream header, each block header, the index, the footer
    </td>
  </tr>
  
  <tr>
    <td>
      zstd
    </td>
    
    <td>
      the low 32 bits of XXH64
    </td>
    
    <td>
      the frame's output, when the flag is set
    </td>
  </tr>
  
  <tr>
    <td>
      lz4 frame
    </td>
    
    <td>
      XXH32
    </td>
    
    <td>
      the header, each block when flagged, the content when flagged
    </td>
  </tr>
</tbody>
</table>

Raw deflate, .lzma, brotli, lz4 legacy and Unix compress carry none. A damaged stream in one of those can decode into garbage without a word. That's the format, not this package. `identify` scores them lower for exactly that reason.

## Where the checksums come from

From [`@agntn/hashes`](https://hashes.agntn.dev), not from this package. The same code that computes `crc32`, `crc64`, `adler32` and `xxhash` there checks the streams here, so a fix lands in one place. A few of them came in for this package: CRC-32/BZIP2 is `crc32` with `variant: "bzip2"`, CRC-64/XZ is `crc64`, XXH32 is `xxhash` with `bits: 32`.

## Sizes count too

gzip stores the size modulo 2³², zstd and lz4 can store the content size, xz stores sizes in the block headers and the index. Each one is held against what actually came out, and a mismatch is a `ChecksumError` as well. A stream that decodes cleanly but to the wrong length is still a broken stream.


## Sitemap

See the full [sitemap](https://compressions.agntn.dev/sitemap.md) for all pages.
