cybersecurity · intermediate · ~15 min · safe pentest lab

Build a 32-bit checksum tool

Build a streaming checksum.

Challenge

Compute Fletcher's 32-bit checksum over a block of bytes — a fast, non-cryptographic integrity check made of two running sums.

Task

Implement uint32_t fletcher32(const unsigned char *data, size_t n) using the exact variant pinned below.

Variant pinned for this exercise

  • Bytes are packed into 16-bit words little-endian: data[i] | (data[i+1] << 8).
  • For odd-length input, the final lone byte is read as a half-word in the low byte (high byte 0).
  • Both sums start at 0; after each word do sum1 = (sum1 + word) % 65535 then sum2 = (sum2 + sum1) % 65535.
  • The result is (sum2 << 16) | sum1.

Input

  • data: a byte buffer the grader provides (the fixtures are short fixed strings).
  • n: the number of bytes in data.

Output

Returns the 32-bit checksum as a uint32_t.

Example

fletcher32("",    0)   ->   0x00000000
fletcher32("abc", 3)   ->   0xC52562C4
fletcher32("aaaa",4)   ->   0x2424C2C2

Edge cases

  • n == 0: returns 0.
  • Odd n: the trailing byte is processed as one half-word.

Rules

  • This is a non-cryptographic checksum (corruption detection only). For tamper detection use a cryptographic hash like SHA-256.

Input format

A byte buffer data and its length n (fixtures are short fixed strings).

Output format

The 32-bit Fletcher checksum (sum2 << 16) | sum1, as a uint32_t.

Constraints

Use the pinned little-endian-word variant; both sums modulo 65535.

Starter code

#include <stdint.h>
#include <stddef.h>

uint32_t fletcher32(const unsigned char *data, size_t n) {
    /* TODO */
    return 0;
}

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