cybersecurity · intermediate · ~12 min

Compute a CRC-32 checksum

Implement a reflected CRC with the bitwise polynomial loop.

Challenge

Your job

#include <stdint.h>
#include <stddef.h>
uint32_t crc32(const uint8_t *data, size_t n);

Compute the standard IEEE CRC-32: init 0xFFFFFFFF, reflected polynomial 0xEDB88320, final XOR 0xFFFFFFFF. Known answers: ""0x00000000, "123456789"0xCBF43926.

Hints

  1. For each byte: crc ^= byte, then 8× crc = (crc>>1) ^ (poly & -(crc&1)).
  2. Return ~crc.

Why this matters

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

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.