Skip to content

LeviSchuck/tiny-encodings

Repository files navigation

Tiny Encodings

A companion library to join a few other tiny-* libraries.

This library provides reasonably fast implementations of:

  • Base64 Standard Encoding
  • Base64 URL Encoding
  • Base64 Standard Decoding
  • Base64 URL Decoding
  • Hex / Base16 Encoding
  • Hex / Base16 Decoding

Where decoding functions take strings and output a Uint8Arrays, and encoding functions take any buffer type (ArrayBuffer, Uint8Array, other typed arrays, and DataViews) and output a string.

This library follows the byte order of the host, this means that typed arrays such as Uint32Array will be encoded in the byte order in its underlying memory buffer which is host specific. If platform compatibility is required for multi-byte buffers, see arrayToEndian and arrayFromEndian.

Initially, this library was going to be the reference implementation, as seen in encoding_reference.ts. However, the reference implementation performance is abysmal.

Tiny-encodings mitigates Base64 Malleability by refusing mangled encoded inputs.

// NPM
// import { decodeBase64 } from "@levischuck/tiny-encodings";
// or Deno
// import { decodeBase64 } from "https://deno.land/x/tiny_encodings@version/index.ts";
import { decodeBase64 } from "./index.ts";

decodeBase64("SGVsbG8gd29ybGQ=");
// returns a Uint8Array with the bytes for "Hello world"