/base64

Cross-platform base64 methods for encoding and decoding Unicode (16-bit) strings, including methods to make strings safe for use in `application/x-www-form-urlencoded` values.

Primary LanguageJavaScript

base64

Cross-platform base64 methods for encoding and decoding data. This module solves three problems.

  1. It works in Node.js and in any browser with or without btoa(), atob(), Buffers, or TypedArrays.
  2. It handles 16-bit-encoded strings, even those that contain characters exceeding the 8-bit ASCII-encoding range (see The โ€œUnicode Problemโ€).
  3. It provides methods for encoding and decoding with a URL- and filename-safe alphabet with or without '=' padding characters so that encoded strings can be safely used with application/x-www-form-urlencoded data, including as part of URL query strings (see Named Information (ni) URI Format Digest Values in RFC 6920).

Installation

$ npm install https://github.com/mikol/base64

Usage

var json = '{"alchemy": "๐Ÿœ˜๐Ÿœ›๐Ÿœœ๐Ÿœ๐Ÿœž๐ŸœŸ๐Ÿœ ๐Ÿœก๐Ÿœฃ๐Ÿœค๐Ÿœฅ๐Ÿœจ๐Ÿœฉ๐Ÿœช๐Ÿœซ๐Ÿœฌ๐Ÿœญ๐Ÿœฎ๐Ÿœฏ๐Ÿœฑ"}';

// Use canonical encoding.
var b64 = base64.encode(json);

// eyJhbGNoZW15IjogIvCfnJjwn5yb8J+co/CfnKTwn5yl8J+cqPCfnKnwn5yq8J+cr/CfnLEifQ==

// Use URL- and filename-safe alphabet.
var b64url = base64.url.encode(json);

// eyJhbGNoZW15IjogIvCfnJjwn5yb8J-co_CfnKTwn5yl8J-cqPCfnKnwn5yq8J-cr_CfnLEifQ==

// Use URL- and filename-safe alphabet. Omit '=' padding characters.
var b64ni = base64.ni.encode(json);

// eyJhbGNoZW15IjogIvCfnJjwn5yb8J-co_CfnKTwn5yl8J-cqPCfnKnwn5yq8J-cr_CfnLEifQ

// Decode. One method to rule them all.
base64.decode(b64);
base64.decode(b64url);
base64.decode(b64ni);