# rfc4648

> Encoding and decoding for base64, base32, base16, and friends

Latest version **1.5.4** (published 2024-12-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install rfc4648
pnpm add rfc4648
yarn add rfc4648
bun add rfc4648
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 1.5.4 |
| Published | 2024-12-10 |
| First published | 2017-04-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 19.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 74 |
| Author | William Swanson |
| Maintainers | swansontec |
| Keywords | Uint8Array, base16, base32, base32hex, base64, base64url, hex |

## Links

- npm: https://www.npmjs.com/package/rfc4648
- Repository: https://github.com/swansontec/rfc4648.js
- Homepage: https://github.com/swansontec/rfc4648.js#readme
- Issues: https://github.com/swansontec/rfc4648.js/issues
- npm.io page: https://npm.io/package/rfc4648

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 1.5.4 (latest) — 2024-12-10
- 1.5.3 — 2023-10-27
- 1.5.2 — 2022-05-30
- 1.5.1 — 2022-01-04
- 1.5.0 — 2021-05-25
- 1.4.0 — 2020-07-10
- 1.3.0 — 2019-09-25
- 1.2.1 — 2019-05-24
- 1.2.0 — 2019-04-05
- 1.1.0 — 2018-03-28
- 1.0.0 — 2017-06-09
- 0.9.1 — 2017-04-11
- 0.9.0 — 2017-04-11

## README

# rfc4648.js

[![Build Status](https://travis-ci.com/swansontec/rfc4648.js.svg?branch=master)](https://travis-ci.com/swansontec/rfc4648.js)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

This library implements encoding and decoding for the data formats specified in [rfc4648](https://tools.ietf.org/html/rfc4648):

- base64
- base64url
- base32
- base32hex
- base16

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

```js
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](https://rollupjs.org/) or [Webpack 2+](https://webpack.js.org/) 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:

```js
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 (`=`):

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

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

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

### Custom encodings

To define your own encodings, use the `codec` module:

```js
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.

---
_Source: https://npm.io/package/rfc4648 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
