1.5.3 • Published 6 months ago

rfc4648 v1.5.3

Weekly downloads
65,405
License
MIT
Repository
github
Last release
6 months ago

rfc4648.js

Build Status JavaScript Style Guide

This library implements encoding and decoding for the data formats specified in rfc4648:

  • base64
  • base64url
  • base32
  • base32hex
  • base16

Each encoding has a simple API inspired by Javascript's built-in JSON object:

import { base32 } from "rfc4648";

base32.stringify([42, 121, 160]); // -> 'FJ42A==='
base32.parse("FJ42A==="); // -> Uint8Array([42, 121, 160])

The library has tree-shaking support, so tools like rollup.js or Webpack 2+ can automatically trim away any encodings you don't use.

  • Zero external dependencies
  • 100% test coverage
  • Built-in types for Typescript & Flow
  • 0.8K minified + gzip (can be even smaller with tree shaking)

API details

The library provides the following top-level modules:

  • base64
  • base64url
  • base32
  • base32hex
  • base16
  • codec

Each module exports a parse and stringify function.

const string = baseXX.stringify(data, opts)

Each stringify function takes array-like object of bytes and returns a string.

If you pass the option { pad: false } in the second parameter, the encoder will not output padding characters (=).

const data = baseXX.parse(string, opts)

Each parse function takes a string and returns a Uint8Array of bytes. If you would like a different return type, such as plain Array or a Node.js Buffer, pass its constructor in the second argument:

base64.parse("AOk=", { out: Array });
base64.parse("AOk=", { out: Buffer.allocUnsafe });

The constructor will be called with new, and should accept a single integer for the output length, in bytes.

If you pass the option { loose: true } in the second parameter, the parser will not validate padding characters (=):

base64.parse("AOk", { loose: true }); // No error

The base32 codec will also fix common typo characters in loose mode:

base32.parse("He1l0==", { loose: true }); // Auto-corrects as 'HELLO==='

Custom encodings

To define your own encodings, use the codec module:

const codec = require("rfc4648").codec;

const myEncoding = {
  chars: "01234567",
  bits: 3
};

codec.stringify([220, 10], myEncoding); // '670050=='
codec.parse("670050", myEncoding, { loose: true }); // [ 220, 10 ]

The encoding structure should have two members, a chars member giving the alphabet and a bits member giving the bits per character. The codec.parse function will extend this with a third member, codes, the first time it's called. The codes member is a lookup table mapping from characters back to numbers.

@lucaapp/web-crypto@llkennedy/httpgrpc@monid/vaulted-key-provider@textury/arlocal@infinitebrahmanuniverse/nolb-rf@reflame/lib-resourcejam3-react-sdkrsshub@everything-registry/sub-chunk-2665@msiemens/keycloakify-fix@gmanavarro/bls12381-key-pair@kubernetes/client-node@llkennedy/mercury@llkennedy/protoc-gen-tsjson@recro/easy-kubernetes-controller@ryanclark/k8sweb-auth-library@perlboy/cfworker-jwt@rarimo/zkp-iden3@ramp-network/crypto-address-validatorvoluptasrerum@robertlong/matrix-react-sdk@oneki/utils@okkema/workersapientstone-soup@starportal/cf-workers-openid-connect@starportal/cf-workers-session-store@pito-plugin/media@mtbl/matrix-react-sdkreact-native-safe-cryptoreact-native-piratechain@maccman/web-auth-libraryreact-native-zcash@loomat/utils@mlytics/core-ts@misskey-dev/node-http-message-signaturesreact-native-airbitz-ioreact-native-fast-crypto@mser/matrix-react-sdk@journyio/kubernetes-client@jolocom/vaulted-key-provider@meistrari/gpt-tokenizer@mfellner/react-native-fast-create-hash@magnetcustomer/keycloakify@lsql/core@lsql/ui@mattrglobal/bls12381-key-pair@kskhasegawa/bls12381-key-pair@kzfk52/cf-access-jwt@ngnjs/libcryptoweb-fs-wrappersshort-crypt@toznysecure/sdk@yamdan/bls12381-key-pair@tuxrampage/k8s-client-node@yovafree/kubernetes-client-node@oneauth/sdk-core@pnpm/crypto.base32-hash@stableness/wabblestudo.js@tejo/akso-client@targecy/sdktalktsy-matrix-react-sdkyaob@swyftx/api-crypto-address-validatoregm96-universaledge-sync-clientedge-core-jsedge-currency-accountbasededge-loginearthstarangular-auth-oidc-client@aspensmonster/doipjsgenerate-api-keylightstands-jsmatrix-react-sdkmobilepassdoipjsdiskletlibvalencelattics-remotely-save@dataswapjs/dataswapjscanvas-api-litebysquare@diva.exchange/divachain@diva.exchange/i2p-sam@faustbrian/node-base16@faustbrian/node-base32@faustbrian/node-base32-hex@faustbrian/node-base64-urlmagic-hehe-npm-4magic-hehe-npm-3gpt-tokenizer@bibigpt/rsshubarvm-bestdeveloperangular-oidc-mod@functionland/rn-borg@functionland/rn-borg-bridge@functionland/rn-fula
1.5.3

6 months ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

3 years ago

1.4.0

4 years ago

1.3.0

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

6 years ago

1.0.0

7 years ago

0.9.1

7 years ago

0.9.0

7 years ago