cybersecurity · intermediate · ~12 min

Compute a CRC-32 checksum

Implement a reflected CRC with the bitwise polynomial loop.

Challenge

Compute the standard IEEE CRC-32 — the integrity check behind zip, PNG, and Ethernet frames.

Task

Implement uint32_t crc32(const uint8_t *data, size_t n) using the IEEE CRC-32 parameters: init 0xFFFFFFFF, reflected polynomial 0xEDB88320, final XOR 0xFFFFFFFF.

Input

  • data: a byte buffer the grader passes.
  • n: the number of bytes in data.

Output

Returns the 32-bit CRC as a uint32_t.

Example

crc32("", 0)            ->   0x00000000
crc32("123456789", 9)   ->   0xCBF43926   (canonical check value)
crc32("a", 1)           ->   0xE8B7BE43
crc32("abc", 3)         ->   0x352441C2

Edge cases

  • An empty buffer yields 0x00000000.
  • The reflected polynomial and the init/final all-ones XOR are what make the canonical check value come out — a non-reflected polynomial gives a different result.

Why this matters

CRC-32 (IEEE) is the integrity check behind zip, png, and ethernet frames. Implementing it nails the reflected-polynomial bit loop.

Input format

A byte buffer data and its length n.

Output format

A uint32_t: the IEEE CRC-32 of the input.

Constraints

Init 0xFFFFFFFF, reflected poly 0xEDB88320, final XOR 0xFFFFFFFF.

Starter code

#include <stdint.h>
#include <stddef.h>
uint32_t crc32(const uint8_t *data, size_t n) {
    /* TODO */
    (void)data; (void)n;
    return 0;
}

Common mistakes

Wrong (non-reflected) polynomial. Forgetting init 0xFFFFFFFF or the final ~. Signed-shift surprises.

Edge cases to handle

Empty input → 0. The canonical "123456789" check value.

Complexity

O(n).

Background lessons

Up next

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.