Node.js Reference ยท crypto
Node.js zlib.crc32() method
Reference for zlib.crc32() in Node.js: compute the CRC-32 checksum of a string or buffer, update it incrementally, and learn what it can and cannot protect.
zlib.crc32(data[, value]) computes the CRC-32 checksum of a string or buffer and returns it as an unsigned 32-bit number. This is the same CRC used by gzip, ZIP, PNG and Ethernet, so it lets you verify or generate those checksums yourself. It is fast, needs no streams and can be updated piece by piece by passing the previous result as the starting value.
A CRC detects accidental corruption, such as a flipped bit during transfer. It is not a cryptographic hash: anyone can craft data with a chosen CRC, so never use it for security or as a unique identifier. For those jobs use crypto.createHash('sha256').
zlib.crc32() was added in Node.js 22.2 and 20.15.
import { crc32 } from 'node:zlib'; console.log(crc32('hello world')); // 222957957
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
zlib.crc32(data: string | Buffer | TypedArray | DataView, value?: number): number
| Name | Description |
|---|---|
data |
The bytes to checksum. A string is encoded as UTF-8. |
value |
Optional starting value: the CRC of everything processed so far. Must be an unsigned 32-bit integer. Default 0. |
Returns: an unsigned 32-bit integer (0 to 4294967295), not a signed one, so it can print larger than 2147483647.
Errors: ERR_INVALID_ARG_TYPE if data is not a string or binary view (numbers and plain arrays are rejected) or value is not a number; ERR_OUT_OF_RANGE if value is outside the unsigned 32-bit range.
Usage notes
- The algorithm is CRC-32/ISO-HDLC with polynomial 0xEDB88320 (reflected). The checksum of the ASCII text
123456789is0xcbf43926, which is the standard value for testing implementations. - Chaining works because the result of one call is a valid starting value for the next:
crc32(b, crc32(a))equalscrc32(a + b). - Strings are hashed as UTF-8 bytes. The same text in another encoding produces a different CRC, so convert to a
Bufferfirst when the byte layout matters. - CRC-32 catches all single-bit errors and most bursts shorter than 32 bits, but with 32 bits of output it collides after roughly 77,000 random inputs. Do not use it to deduplicate.
- Use it for ZIP entries, PNG chunks, simple file integrity checks and cache validation inside a trusted system.
- Before Node 22.2 you had to use a userland package or compute the table yourself; this function removes that need.
- It is synchronous but linear and very fast (gigabytes per second), so calling it on a large buffer is usually fine on the main thread.