Base32 Encoding for JavaScript

0.1.0 · abandoned · verified Sun Apr 19

This package provides implementations for various Base32 encoding schemes, including RFC 4648 and Crockford's Base32, tailored for JavaScript environments. Base32 offers distinct advantages over Base64, such as case-insensitivity, suitability for filenames (as it avoids the '/' character), and an alphabet carefully designed to minimize ambiguous characters (e.g., omitting 1, 8, 0 to prevent confusion with I, B, O). However, this comes at the cost of increased data size, with Base32 representations being approximately 20% larger than Base64. The package is currently at version 0.1.0, suggesting it is either an early-stage project or one that has not seen significant updates or active maintenance in a considerable amount of time, lacking a clear release cadence. It differentiates itself by supporting multiple Base32 variants and providing pre-built files for browser compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates encoding and decoding binary data using Crockford Base32 and the default RFC4648 (no padding) variant, showcasing the streaming API.

const base32 = require("base32.js");

console.log("Encoding a buffer using Crockford Base32 (lowercase)...");
const buf = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // Extended buffer for a more realistic example
const encoder = new base32.Encoder({ type: "crockford", lc: true });
const encodedStr = encoder.write(buf).finalize();
console.log(`Original buffer: [${buf.join(', ')}]`);
console.log(`Encoded string: ${encodedStr}`);

console.log("\nDecoding the string back...");
const decoder = new base32.Decoder({ type: "crockford" });
const decodedBuf = decoder.write(encodedStr).finalize();
console.log(`Decoded buffer: [${decodedBuf.join(', ')}]`);

// Example with default RFC4648 without padding
console.log("\nEncoding with default RFC4648 (no padding)...");
const rfc4648Buf = Buffer.from("Hello World"); // Using Buffer for wider compatibility
const rfcEncoder = new base32.Encoder(); // Default is rfc4648, no padding
const rfcEncodedStr = rfcEncoder.write(rfc4648Buf).finalize();
console.log(`Original string: ${rfc4648Buf.toString()}`);
console.log(`Encoded RFC4648: ${rfcEncodedStr}`);

const rfcDecoder = new base32.Decoder();
const rfcDecodedBuf = rfcDecoder.write(rfcEncodedStr).finalize();
console.log(`Decoded RFC4648: ${rfcDecodedBuf.toString()});

view raw JSON →