liveBook · Manning

Checksum

As explained in API Design Patterns

A checksum value is derived data appended to an identifier to help verify that the identifier was received or entered correctly.

A checksum is derived data appended to an identifier to detect copying or entry errors. Recalculating it distinguishes an identifier that points to no resource from one that could never be valid. section 6.2.6 section 6.3.4

A typical design derives fixed checksum characters from the identifier’s content, appends them for display, and recalculates them when an identifier arrives. A mismatch indicates corruption or a likely typo, so the identifier should be rejected rather than treated as a missing resource. section 6.3.4 section 6.4.4

For a Crockford Base32 identifier:

  • Treat the byte value as an integer and calculate its remainder modulo 37. section 6.3.4
  • Map the result to a Base37 character, using the Base32 alphabet plus checksum characters. section 6.4.4
  • Append the character to the encoded identifier and split it from the identifier before verification. section 6.4.4
  • Store the identifier without the checksum character; calculate it for incoming values and append it for outgoing values. section 6.4.5

Verifying a customer-facing identifier

For an incoming Base32 identifier, separate the final character, decode the remaining value, calculate its checksum, and compare the resulting character with the supplied one. Return false on a mismatch. An internal UUID can also be exposed as a Base32-encoded value with an appended checksum, combining UUID storage or generation with readable, verifiable identifiers. section 6.4.4 section 6.5

Listing 6.2 Calculating a Base32 checksum value
function calculateChecksum(bytes: Buffer): number {
  const intValue = BigInt(`0x${bytes.toString('hex')}`);   #1
  return Number(intValue % BigInt(37));                    #2
}

The implementation reduces an arbitrary byte buffer to a checksum value with BigInt and modulo 37. section 6.4.4

How API Design Patterns covers it

How deep it goes
Explains checksum derivation, character encoding, generation, verification, UUID integration, and database handling, but not collision probabilities in depth.
Examples
Demonstrates modulo-37 calculation, Base37 character mapping, generated identifiers, verification, and Base32-encoded UUID v4 values.
What it assumes
Assumes resource identifiers and Base32 encoding, while teaching checksum calculation, encoding, parsing, and verification.
What you can do after
Diagnose identifier typos by recalculating a checksum and reject values whose supplied character does not match.
How it connects
Connects checksums with resource identifiers, Crockford’s Base32, UUIDs, readability, and database storage.

Also explained in

Compare how 5 books explain checksum →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of API Design Patterns. The section links point to the parts of the book it draws on.