root WRITEUP self-sha256

self-sha256

A program that prints its own SHA-256 checksum 2024-06-29

Project Files • GitHub Repo • self-sha256.c • self-sha256.py

Writing a program that outputs its own checksum may sound like a bit of a self-referential mess. It turns out to be trivial because constructive proofs of Kleene’s fixed-point theorem effectively provide us a step-by-step guide on writing programs that perform some transformation on their own source code. In practice, this boils down to writing a constructive quine that outputs to some internal buffer then prints the checksum of its contents. Though when put like that it loses some of its magic :)

The C99 implementation does this in a streaming fashion to avoid allocating an intermediate buffer. The Python implementation is, on a high level, equivalent, though its simplicity obscures the general construction and makes it unobvious that one exists.

For some more information, refer to the readme.