Secure, audited & 0-deps implementation of bech32, base64, base32, base16 & base58
See the codeAudited & minimal implementation of bech32, base64, base58, base32 & base16.
scure β audited micro-libraries.
npm install @scure/base
deno add jsr:@scure/base
We support all major platforms and runtimes. The library is hybrid ESM / Common.js package.
Every codec has the same API: encode(bytes: Uint8Array): string and
decode(str: string): Uint8Array. Invalid input throws Error β
see Strict decoding. Exceptions are bech32 / bech32m
(words-based API) and base58check (requires external sha256); both are
described below.
| Codec | Spec | Padding | Notes |
|---|---|---|---|
base16 | RFC 4648 Β§8 | β | Uppercase; decode is case-sensitive. Use hex for case-insensitive decoding |
hex | β | β | Lowercase; decode accepts both cases. Native Uint8Array.fromHex when available |
base32, base32nopad | RFC 4648 Β§6 | = / no | Uppercase; decode is case-sensitive |
base32hex, base32hexnopad | RFC 4648 Β§7 | = / no | Different alphabet ordering than base32 |
base32crockford | Crockford | β | Decode is case-insensitive and normalizes Oβ0, Iβ1, Lβ1 |
base64, base64nopad | RFC 4648 Β§4 | = / no | Native Uint8Array.fromBase64 when available |
base64url, base64urlnopad | RFC 4648 Β§5 | = / no | URL-safe alphabet; native builtin when available |
base58, base58flickr, base58xrp | draft-msporny | β | O(nΒ²), only for small constant-size inputs β see below |
base58xmr | Monero | β | Processes 8-byte blocks, which makes it linear |
base36 | multibase k (IPNS, CIDv1) | β | Lowercase alphanumeric; O(nΒ²), same constraints as base58 |
createBase58check(sha256) | Base58check | β | Returns a codec; caller injects sha256 |
bech32, bech32m | BIP173, BIP350 | β | Words-based API with (prefix, words) |
utf8, ascii | β | β | String β bytes coders, strict validation |
import { base16, base32, base64, base58 } from '@scure/base';
// Flavors
import {
base36,
base58flickr,
base58xmr,
base58xrp,
base32nopad,
base32hex,
base32hexnopad,
base32crockford,
base64nopad,
base64url,
base64urlnopad,
hex,
} from '@scure/base';
const data = Uint8Array.from([1, 2, 3]);
base64.decode(base64.encode(data));
// Convert utf8 string to Uint8Array
const data2 = new TextEncoder().encode('hello');
base58.encode(data2);
// Everything has the same API except for bech32 and base58check
base32.encode(data);
base16.encode(data);
base32hex.encode(data);
Also, the internal export __TESTS.base64Fallback is identical to base64, but never uses native code.
base58check is a special case: you need to pass sha256() function:
npm install @noble/hashes
import { createBase58check } from '@scure/base';
import { sha256 } from '@noble/hashes/sha2.js';
const base58check = createBase58check(sha256);
const data = Uint8Array.from([1, 2, 3]);
base58check.encode(data);
import { bech32, bech32m } from '@scure/base';
const words = bech32.toWords(new TextEncoder().encode('hello'));
const addr = bech32.encode('test', words);
console.log(addr); // "test1dpjkcmr09ys0qs"
const { prefix, words: decoded } = bech32.decode(addr);
console.log(prefix); // "test"
console.log(new TextDecoder().decode(bech32.fromWords(decoded))); // "hello"
console.log(bech32.decodeUnsafe('invalid')); // undefined, instead of throwing
// bech32m (BIP350) has the same API
bech32m.decode(bech32m.encode('test', words));
// Shortcuts which convert bytes to words and back for you
const addr2 = bech32.encodeFromBytes('test', new TextEncoder().encode('hello'));
const { bytes } = bech32.decodeToBytes(addr2);
Per BIP173, encoded strings are limited to 90 characters. encode, decode,
and decodeToBytes accept an optional limit argument to raise the limit
(Lightning invoices are longer), or false to disable it entirely.
We provide low-level bech32 operations. If you need high-level methods for BTC (addresses, and others), use scure-btc-signer instead.
Bitcoin addresses use both 5-bit words and bytes representations.
They can't be parsed using bech32.decodeToBytes.
Same applies to Lightning Invoice Protocol
BOLT-11.
We have many tests in ./test/bip173.test.ts that serve as minimal examples of
Bitcoin address and Lightning Invoice Protocol parsers.
Keep in mind that you'll need to verify the examples before using them in your code.
Do something like this:
import { bech32 } from '@scure/base';
const address = bech32.encode('bc', [0, ...bech32.toWords(new Uint8Array(20))]);
const decoded = bech32.decode(address);
// NOTE: words in bitcoin addresses contain version as first element,
// with actual witness program words in rest
// BIP-141: The value of the first push is called the "version byte".
// The following byte vector pushed is called the "witness program".
const [version, ...dataW] = decoded.words;
const program = bech32.fromWords(dataW); // actual witness program
hex, utf8 and ascii are string β bytes coders. They follow the same
BytesCoder interface, so encode takes bytes and returns a string,
while decode takes a string and returns bytes:
import { hex, utf8, ascii } from '@scure/base';
hex.decode('0102ff'); // Uint8Array([1, 2, 255]); accepts "0102FF" too
hex.encode(Uint8Array.from([1, 2, 255])); // "0102ff", always lowercase
utf8.decode('hey'); // Uint8Array([104, 101, 121])
utf8.encode(Uint8Array.from([104, 101, 121])); // "hey"
ascii.decode('ABC'); // Uint8Array([65, 66, 67])
Unlike base16, hex emits lowercase and decodes either case, matching
Node.js Buffer hex strings. utf8 is strict: it throws on invalid UTF-8
bytes and on malformed UTF-16 strings with lone surrogates, instead of
silently emitting replacement characters. ascii rejects anything outside
of 7-bit ASCII.
hex, base64 and base64url transparently use native
Uint8Array.fromHex / fromBase64 builtins when the runtime provides them,
falling back to the pure-JS implementation otherwise. Validation behavior is
identical on both paths.
If decode(str) succeeds, the input was canonical, with a small documented
set of normalizations (hex and base32crockford case-folding, bech32
accepting all-uppercase strings). Everything else throws:
base64.decode('aGk=\n') throwsbase64.decode('aGk') throws (use base64nopad)base64.decode('aGl=') throwsdecode or a non-Uint8Array to encode throwsThis matters for security-sensitive code: malleable encodings enable
transaction-ID and signature malleability, cache-poisoning and
double-spending of allowlists. It also guarantees encode(decode(str))
either returns an equivalent canonical string or throws β unlike atob,
which accepts and silently normalizes malformed input.
The code may look unnecessarily complicated; but the structure actually makes it much easier to reason about. Any encoding library consists of two functions:
encode(A) -> B
decode(B) -> A
where X = decode(encode(X))
# encode(decode(X)) can be !== X!
# because decoding can normalize input
e.g.
base58checksum = {
encode(): {
// checksum
// radix conversion
// alphabet
},
decode(): {
// alphabet
// radix conversion
// checksum
}
}
But instead of creating two big functions for each specific case,
we create them from tiny composable building blocks
(alphabet, radix, padding, checksum, chain):
base58checksum = chain(checksum(), radix(), alphabet())
Which is the same as chain/pipe/sequence function in Functional Programming, but significantly more useful since it enforces same order of execution of encode/decode. Basically you only define encode (in declarative way) and get correct decode for free. So, instead of reasoning about two big functions you need only reason about primitives and encode chain. The design revealed obvious bug in older version of the lib, where xmr version of base58 had errors in decode's block processing.
Uint8Array is represented as big-endian number:
[1, 2, 3, 4, 5] -> 1*(256**4) + 2*(256**3) 3*(256**2) + 4*(256**1) + 5*(256**0)
where 256 = 2**8 (8 bits per byte)
which is then converted to a number in another radix/base (16/32/58/64, etc).
However, generic conversion between bases has quadratic O(n^2) time complexity.
Which means base58 has quadratic time complexity too. Use base58 only when you have small
constant sized input, because variable length sized input from user can cause DoS.
The same applies to base36, which shares the implementation.
Both reject inputs over 2,048 bytes and encoded strings over 4,096 characters before conversion.
On the other hand, if both bases are power of same number (like 2**8 <-> 2**64),
there is linear algorithm. For now we have implementation for power-of-two bases only (radix2).
base58xmr sidesteps the issue differently: it encodes in fixed 8-byte blocks,
which keeps it linear at the cost of a slightly different (Monero-specific) format.
The library has been audited:
The library was initially developed for js-ethereum-cryptography.
At commit ae00e6d7,
it was extracted to a separate package called micro-base.
After the audit we've decided to use @scure NPM namespace for security.
For this package, there are 0 dependencies; and a few dev dependencies:
Run benchmarks with npm run benchmark. Results on Apple M4, Node.js v24:
base16 encode x 344 mib/sec
base16 decode x 142 mib/sec
base32 encode x 427 mib/sec
base32 decode x 165 mib/sec
base64 encode x 18.7 gib/sec
base64 decode x 6.63 gib/sec
base64nopad encode x 486 mib/sec
base64nopad decode x 214 mib/sec
base58 encode 32 B x 32.2 mib/sec
base58 decode 32 B x 60.2 mib/sec
utf8 encode x 1.81 gib/sec
utf8 decode x 1.78 gib/sec
bech32 toWords x 351 mib/sec
bech32 fromWords x 161 mib/sec
MIT (c) Paul Miller (https://paulmillr.com), see LICENSE file.
3,743 followers Β· starred Apr 2026
384 followers Β· starred Oct 2023
275 followers Β· starred Aug 2026
288 followers Β· starred Jul 2024
Secure, audited & 0-deps implementation of bech32, base64, base32, base16 & base58
See the codeAudited & minimal implementation of bech32, base64, base58, base32 & base16.
scure β audited micro-libraries.
npm install @scure/base
deno add jsr:@scure/base
We support all major platforms and runtimes. The library is hybrid ESM / Common.js package.
Every codec has the same API: encode(bytes: Uint8Array): string and
decode(str: string): Uint8Array. Invalid input throws Error β
see Strict decoding. Exceptions are bech32 / bech32m
(words-based API) and base58check (requires external sha256); both are
described below.
| Codec | Spec | Padding | Notes |
|---|---|---|---|
base16 | RFC 4648 Β§8 | β | Uppercase; decode is case-sensitive. Use hex for case-insensitive decoding |
hex | β | β | Lowercase; decode accepts both cases. Native Uint8Array.fromHex when available |
base32, base32nopad | RFC 4648 Β§6 | = / no | Uppercase; decode is case-sensitive |
base32hex, base32hexnopad | RFC 4648 Β§7 | = / no | Different alphabet ordering than base32 |
base32crockford | Crockford | β | Decode is case-insensitive and normalizes Oβ0, Iβ1, Lβ1 |
base64, base64nopad | RFC 4648 Β§4 | = / no | Native Uint8Array.fromBase64 when available |
base64url, base64urlnopad | RFC 4648 Β§5 | = / no | URL-safe alphabet; native builtin when available |
base58, base58flickr, base58xrp | draft-msporny | β | O(nΒ²), only for small constant-size inputs β see below |
base58xmr | Monero | β | Processes 8-byte blocks, which makes it linear |
base36 | multibase k (IPNS, CIDv1) | β | Lowercase alphanumeric; O(nΒ²), same constraints as base58 |
createBase58check(sha256) | Base58check | β | Returns a codec; caller injects sha256 |
bech32, bech32m | BIP173, BIP350 | β | Words-based API with (prefix, words) |
utf8, ascii | β | β | String β bytes coders, strict validation |
import { base16, base32, base64, base58 } from '@scure/base';
// Flavors
import {
base36,
base58flickr,
base58xmr,
base58xrp,
base32nopad,
base32hex,
base32hexnopad,
base32crockford,
base64nopad,
base64url,
base64urlnopad,
hex,
} from '@scure/base';
const data = Uint8Array.from([1, 2, 3]);
base64.decode(base64.encode(data));
// Convert utf8 string to Uint8Array
const data2 = new TextEncoder().encode('hello');
base58.encode(data2);
// Everything has the same API except for bech32 and base58check
base32.encode(data);
base16.encode(data);
base32hex.encode(data);
Also, the internal export __TESTS.base64Fallback is identical to base64, but never uses native code.
base58check is a special case: you need to pass sha256() function:
npm install @noble/hashes
import { createBase58check } from '@scure/base';
import { sha256 } from '@noble/hashes/sha2.js';
const base58check = createBase58check(sha256);
const data = Uint8Array.from([1, 2, 3]);
base58check.encode(data);
import { bech32, bech32m } from '@scure/base';
const words = bech32.toWords(new TextEncoder().encode('hello'));
const addr = bech32.encode('test', words);
console.log(addr); // "test1dpjkcmr09ys0qs"
const { prefix, words: decoded } = bech32.decode(addr);
console.log(prefix); // "test"
console.log(new TextDecoder().decode(bech32.fromWords(decoded))); // "hello"
console.log(bech32.decodeUnsafe('invalid')); // undefined, instead of throwing
// bech32m (BIP350) has the same API
bech32m.decode(bech32m.encode('test', words));
// Shortcuts which convert bytes to words and back for you
const addr2 = bech32.encodeFromBytes('test', new TextEncoder().encode('hello'));
const { bytes } = bech32.decodeToBytes(addr2);
Per BIP173, encoded strings are limited to 90 characters. encode, decode,
and decodeToBytes accept an optional limit argument to raise the limit
(Lightning invoices are longer), or false to disable it entirely.
We provide low-level bech32 operations. If you need high-level methods for BTC (addresses, and others), use scure-btc-signer instead.
Bitcoin addresses use both 5-bit words and bytes representations.
They can't be parsed using bech32.decodeToBytes.
Same applies to Lightning Invoice Protocol
BOLT-11.
We have many tests in ./test/bip173.test.ts that serve as minimal examples of
Bitcoin address and Lightning Invoice Protocol parsers.
Keep in mind that you'll need to verify the examples before using them in your code.
Do something like this:
import { bech32 } from '@scure/base';
const address = bech32.encode('bc', [0, ...bech32.toWords(new Uint8Array(20))]);
const decoded = bech32.decode(address);
// NOTE: words in bitcoin addresses contain version as first element,
// with actual witness program words in rest
// BIP-141: The value of the first push is called the "version byte".
// The following byte vector pushed is called the "witness program".
const [version, ...dataW] = decoded.words;
const program = bech32.fromWords(dataW); // actual witness program
hex, utf8 and ascii are string β bytes coders. They follow the same
BytesCoder interface, so encode takes bytes and returns a string,
while decode takes a string and returns bytes:
import { hex, utf8, ascii } from '@scure/base';
hex.decode('0102ff'); // Uint8Array([1, 2, 255]); accepts "0102FF" too
hex.encode(Uint8Array.from([1, 2, 255])); // "0102ff", always lowercase
utf8.decode('hey'); // Uint8Array([104, 101, 121])
utf8.encode(Uint8Array.from([104, 101, 121])); // "hey"
ascii.decode('ABC'); // Uint8Array([65, 66, 67])
Unlike base16, hex emits lowercase and decodes either case, matching
Node.js Buffer hex strings. utf8 is strict: it throws on invalid UTF-8
bytes and on malformed UTF-16 strings with lone surrogates, instead of
silently emitting replacement characters. ascii rejects anything outside
of 7-bit ASCII.
hex, base64 and base64url transparently use native
Uint8Array.fromHex / fromBase64 builtins when the runtime provides them,
falling back to the pure-JS implementation otherwise. Validation behavior is
identical on both paths.
If decode(str) succeeds, the input was canonical, with a small documented
set of normalizations (hex and base32crockford case-folding, bech32
accepting all-uppercase strings). Everything else throws:
base64.decode('aGk=\n') throwsbase64.decode('aGk') throws (use base64nopad)base64.decode('aGl=') throwsdecode or a non-Uint8Array to encode throwsThis matters for security-sensitive code: malleable encodings enable
transaction-ID and signature malleability, cache-poisoning and
double-spending of allowlists. It also guarantees encode(decode(str))
either returns an equivalent canonical string or throws β unlike atob,
which accepts and silently normalizes malformed input.
The code may look unnecessarily complicated; but the structure actually makes it much easier to reason about. Any encoding library consists of two functions:
encode(A) -> B
decode(B) -> A
where X = decode(encode(X))
# encode(decode(X)) can be !== X!
# because decoding can normalize input
e.g.
base58checksum = {
encode(): {
// checksum
// radix conversion
// alphabet
},
decode(): {
// alphabet
// radix conversion
// checksum
}
}
But instead of creating two big functions for each specific case,
we create them from tiny composable building blocks
(alphabet, radix, padding, checksum, chain):
base58checksum = chain(checksum(), radix(), alphabet())
Which is the same as chain/pipe/sequence function in Functional Programming, but significantly more useful since it enforces same order of execution of encode/decode. Basically you only define encode (in declarative way) and get correct decode for free. So, instead of reasoning about two big functions you need only reason about primitives and encode chain. The design revealed obvious bug in older version of the lib, where xmr version of base58 had errors in decode's block processing.
Uint8Array is represented as big-endian number:
[1, 2, 3, 4, 5] -> 1*(256**4) + 2*(256**3) 3*(256**2) + 4*(256**1) + 5*(256**0)
where 256 = 2**8 (8 bits per byte)
which is then converted to a number in another radix/base (16/32/58/64, etc).
However, generic conversion between bases has quadratic O(n^2) time complexity.
Which means base58 has quadratic time complexity too. Use base58 only when you have small
constant sized input, because variable length sized input from user can cause DoS.
The same applies to base36, which shares the implementation.
Both reject inputs over 2,048 bytes and encoded strings over 4,096 characters before conversion.
On the other hand, if both bases are power of same number (like 2**8 <-> 2**64),
there is linear algorithm. For now we have implementation for power-of-two bases only (radix2).
base58xmr sidesteps the issue differently: it encodes in fixed 8-byte blocks,
which keeps it linear at the cost of a slightly different (Monero-specific) format.
The library has been audited:
The library was initially developed for js-ethereum-cryptography.
At commit ae00e6d7,
it was extracted to a separate package called micro-base.
After the audit we've decided to use @scure NPM namespace for security.
For this package, there are 0 dependencies; and a few dev dependencies:
Run benchmarks with npm run benchmark. Results on Apple M4, Node.js v24:
base16 encode x 344 mib/sec
base16 decode x 142 mib/sec
base32 encode x 427 mib/sec
base32 decode x 165 mib/sec
base64 encode x 18.7 gib/sec
base64 decode x 6.63 gib/sec
base64nopad encode x 486 mib/sec
base64nopad decode x 214 mib/sec
base58 encode 32 B x 32.2 mib/sec
base58 decode 32 B x 60.2 mib/sec
utf8 encode x 1.81 gib/sec
utf8 decode x 1.78 gib/sec
bech32 toWords x 351 mib/sec
bech32 fromWords x 161 mib/sec
MIT (c) Paul Miller (https://paulmillr.com), see LICENSE file.
3,743 followers Β· starred Apr 2026
384 followers Β· starred Oct 2023
275 followers Β· starred Aug 2026
288 followers Β· starred Jul 2024