base32kit
v0.1.1
Published
Tiny, isomorphic Base32 codec — RFC 4648, base32hex, and Crockford, between string and Uint8Array. Zero dependencies.
Maintainers
Readme
base32kit
Tiny, isomorphic Base32 codec — RFC 4648, base32hex, and Crockford, between strings and
Uint8Array. Zero dependencies.
Buffer doesn't do Base32, and you reach for it when handling TOTP/2FA
secrets (RFC 4648) or Crockford ids. base32kit covers all three common
alphabets, both directions, between strings and bytes — the same in Node and the
browser. Zero dependencies. (For Base64/hex, see its sibling
codeckit.)
import { encode, decode } from "base32kit";
encode("foobar"); // "MZXW6YTBOI======"
decode("MZXW6YTBOI======"); // "foobar"
encode("id", { variant: "crockford" }); // unpadded CrockfordWhy base32kit?
- Three alphabets.
rfc4648(default,A–Z2–7),hex(base32hex,0–9A–V), andcrockford(0–9A–ZminusI L O U, case-insensitive, no padding). - Tolerant decoding. Case-insensitive, ignores padding, whitespace, and
-; Crockford also acceptsO→0 andI/L→1. - RFC-correct. Passes the RFC 4648 §10 test vectors exactly.
- Bytes or text. Low-level
Uint8Arrayfunctions plus UTF-8 text wrappers. - Isomorphic & tiny. Node, Deno, Bun, browsers. Full types, ESM + CJS, zero dependencies.
Install
npm install base32kit
# or: pnpm add base32kit / yarn add base32kit / bun add base32kitText
import { encode, decode } from "base32kit";
encode("foobar"); // "MZXW6YTBOI======"
encode("foobar", { pad: false }); // "MZXW6YTBOI"
encode("foo", { variant: "hex" }); // "CPNMU==="
decode("mzxw6ytboi", { variant: "rfc4648" }); // "foobar" (case + padding tolerant)Bytes
import { bytesToBase32, base32ToBytes } from "base32kit";
bytesToBase32(new Uint8Array([1, 2, 3, 4, 5])); // RFC 4648
bytesToBase32(secret, { variant: "crockford" }); // unpadded
base32ToBytes("JBSWY3DPEHPK3PXP"); // → Uint8Array
// TOTP secret → bytes
const key = base32ToBytes(otpSecret.replace(/\s/g, ""));interface EncodeOptions {
variant?: "rfc4648" | "hex" | "crockford"; // default "rfc4648"
pad?: boolean; // default: on for rfc4648/hex, off for crockford
}Contributors ✨
This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
License
MIT © Tung Tran
