hash32

tcmts.store.hash32 · #taesiqsp · term · runs on the server

uses node:crypto

import { B32 } from "#codebase/tcmts/store";
import { createHash } from "node:crypto";

export function hash32(domain: string, text: string): string {
  const bytes = createHash("sha256").update(`${domain}\0${text}`).digest();
  let out = "";
  let bits = 0;
  let value = 0;
  for (const byte of bytes) {
    value = (value << 8) | byte;
    bits += 8;
    while (bits >= 5) {
      out += B32[(value >>> (bits - 5)) & 31];
      bits -= 5;
    }
  }
  if (bits > 0) out += B32[(value << (5 - bits)) & 31];
  return out;
}

uses

used by