GitHub - montyanderson/ts-sha256: sha-256 computed entirely by the typescript type checker. zero runtime.

1 min read Original article ↗

sha-256 computed entirely by the typescript type checker. zero runtime.

demo

import type { SHA256 } from "./sha256";

type Hash = SHA256<"Input string">;
//   ^? "da408360ba4fd661aaaacb205dd680fc71280558627f45f99dd2b5f578cbb506"

the type-level program faithfully implements fips 180-4: bits live in 32-tuples of 0 | 1, a ripple-carry adder does mod-2³² addition, the message schedule and 64-round compression run as tail-recursive conditional types. running tsc --noEmit is the hash.

install

use it

drop sha256.ts into your project and import SHA256<S>. that's the whole api.

import type { SHA256 } from "./sha256";

type A = SHA256<"">;     // "e3b0c442..."
type B = SHA256<"abc">;  // "ba7816bf..."

input is a printable-ascii string literal. output is a 64-char lowercase hex string literal. unsupported characters resolve to never.

run the tests

the test vectors are compile-time Assert<Equals<...>> declarations. if tsc exits clean, they passed.

NODE_OPTIONS="--max-old-space-size=12288" pnpm exec tsc --noEmit

the big heap reservation is required — the checker allocates a lot of intermediate tuple types.

limits

  • printable ascii + a few control chars. non-ascii → never.
  • single 512-bit block (≤ 55 bytes) is what i actually tested. longer inputs are implemented but will probably exhaust the type checker.
  • expect ~20s per hash on a modern laptop.