Cross-platform base64 methods for encoding and decoding data. This module solves three problems.
- It works in Node.js and in any browser with or without
btoa()
,atob()
, Buffers, or TypedArrays. - It handles 16-bit-encoded strings, even those that contain characters exceeding the 8-bit ASCII-encoding range (see The “Unicode Problem”).
- 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 withapplication/x-www-form-urlencoded
data, including as part of URL query strings (see Named Information (ni) URI Format Digest Values in RFC 6920).
$ npm install https://github.com/mikol/base64
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);