cybersecurity · intermediate · ~15 min · safe pentest lab

Compare two files by content hash

Stream-compare files via fread chunks.

Challenge

Decide whether two files hold identical bytes, streaming them in chunks rather than slurping each into memory.

Task

Implement int files_match(const char *a, const char *b) that compares the two files byte for byte.

Input

  • a, b: NUL-terminated paths. The grader creates the test files locally before each check.

Output

Returns an int:

  • 1 if both files open and have exactly the same bytes.
  • 0 if both open but differ (in content or length).
  • -1 if either file cannot be opened.

Example

both contain "hello world"   ->   1
"hello world" vs "hello WORLD"   ->   0
one path missing   ->   -1

Edge cases

  • Files of different lengths differ → return 0.
  • Two empty files are equal → return 1.
  • Read in fixed-size chunks; do not load whole files into memory.

Input format

Two NUL-terminated paths a and b to local files the grader created.

Output format

An int: 1 if identical, 0 if both open but differ, -1 if either can't be opened.

Constraints

Stream the comparison in fixed-size chunks; no whole-file loads.

Starter code

#include <stdio.h>

int files_match(const char *a, const char *b) {
    /* TODO */
    return -1;
}

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