Fast, error-resilient, zero-dependency codecs for Dart.
BinaryΒ Β·Β OctalΒ Β·Β HexΒ Β·Β Base-32Β Β·Β Base-64Β Β·Β BigIntΒ Β·Β UTF-8Β Β·Β PHCΒ /Β ModularΒ Crypt
A pure-Dart library of fast, error-resilient codecs: binary, octal, hex, Base-32, Base-64, BigInt, UTF-8, and the PHC / Modular Crypt Format β with a rich set of alphabet variants, optional padding, and zero dependencies.
convertlib is the foundation layer of a three-package family:
Both hashlib (hashes, MACs, KDFs) and cipherlib (ciphers, AEAD, ML-KEM)
build on it, reusing its codecs to render digests, keys, and ciphertext as
text. It carries no runtime dependencies of its own.
| Feature | What you get | |
|---|---|---|
| π | Runs on every platform | Pure Dart with no native code or FFI β the same library works on the VM, Flutter (Android, iOS, Windows, macOS, Linux), and the web (dart2js and dart2wasm). |
| π¦ | Zero dependencies | Nothing is pulled into your dependency tree. |
| π‘ | Broad coverage | Base-2, Base-8, Base-16, Base-32, Base-64, BigInt, UTF-8, and the PHC / Modular Crypt Format, each with a symmetrical to/from pair. |
| ποΈ | Many alphabet variants | RFC 4648 standard, base32hex, Crockford, z-base-32, geohash, word-safe, URL/filename-safe Base-64, and bcrypt. |
| β³οΈ | Optional padding | Encode and decode with or without = padding. |
| π‘οΈ | Error resilient | Decoders accept case and alphabet variations where the encoding allows it, and raise typed errors on genuinely invalid input. |
| π§© | Convenient output | Results integrate with ByteCollector, the digest container shared with hashlib, for one-call re-encoding. |
| βοΈ | Bytes or strings, throwing or not | Every encoder has a to<Name>Bytes twin returning a Uint8List, every decoder a non-throwing tryFrom<Name> returning null, plus a top-level constantTimeEquals. |
dependencies:
convertlib: ^3.6.1or run dart pub add convertlib. A single import exposes every codec:
import 'package:convertlib/convertlib.dart';Full API reference: convertlib library.
Every codec exposes a to<Name> encoder and a from<Name> decoder that are
exact inverses of each other:
import 'package:convertlib/convertlib.dart';
void main() {
final data = toUtf8('convertlib');
// Encode the same bytes in a few common ways
final hex = toHex(data);
final b64 = toBase64(data);
final b32 = toBase32(data);
print('bytes : $data');
print('hex : $hex');
print('base64 : $b64');
print('base32 : $b32');
// Every encoder has an exact inverse
print('decoded: ${fromUtf8(fromHex(hex))}');
print('match : ${fromUtf8(fromBase64(b64)) == 'convertlib'}');
}Every snippet in this README is also a runnable program in the example folder.
| Encoding | Class | Encode | Decode | Source |
|---|---|---|---|---|
| Binary (Base-2) | Base2Codec |
toBinary, toBinaryBytes |
fromBinary, tryFromBinary |
β |
| Octal (Base-8) | Base8Codec |
toOctal, toOctalBytes |
fromOctal, tryFromOctal |
β |
| Hexadecimal (Base-16) | Base16Codec |
toHex, toHexBytes |
fromHex, tryFromHex |
RFC-4648 |
| Base-32 | Base32Codec |
toBase32, toBase32Bytes |
fromBase32, tryFromBase32 |
RFC-4648 |
| Base-64 | Base64Codec |
toBase64, toBase64Bytes |
fromBase64, tryFromBase64 |
RFC-4648 |
| BigInt | BigIntCodec |
toBigInt |
fromBigInt, tryFromBigInt |
β |
| UTF-8 | UTF8Codec |
toUtf8 |
fromUtf8, tryFromUtf8 |
RFC-3629 |
| Modular Crypt Format | CryptFormat |
toCrypt |
fromCrypt |
PHC string format |
Base-16 β toHex(..., upper: true) selects the uppercase alphabet:
Base16Codec.upperβ0123456789ABCDEFBase16Codec.lowerβ0123456789abcdef(default)
Base-32 β pass codec: to pick an alphabet, lower:/padding: to tweak it:
Base32Codec.standard(RFC-4648) βABCDEFGHIJKLMNOPQRSTUVWXYZ234567(default)Base32Codec.lowercaseβabcdefghijklmnopqrstuvwxyz234567Base32Codec.hex/.hexLowerβ base32hex,0-9A-V/0-9a-vBase32Codec.crockfordβ0123456789ABCDEFGHJKMNPQRSTVWXYZ(decoding is case-insensitive and acceptsI/i/L/las1andO/oas0)Base32Codec.geohashβ0123456789bcdefghjkmnpqrstuvwxyzBase32Codec.zβ z-base-32,ybndrfg8ejkmcpqxot1uwisza345h769Base32Codec.wordSafeβ23456789CFGHJMPQRVWXcfghjmpqrvwx
Base-64 β pass url: true for URL/filename-safe output, padding: false
to drop =, or a codec::
Base64Codec.standard(RFC-4648) βA-Za-z0-9+/(default)Base64Codec.urlSafeβA-Za-z0-9-_Base64Codec.bcryptβ./A-Za-z0-9
BigInt β endianness is selectable via msbFirst or an explicit codec:
BigIntCodec.msbFirstβ treats bytes in big-endian orderBigIntCodec.lsbFirstβ treats bytes in little-endian order (default)
- Byte output β the
to<Name>Bytesencoders in the table above return the encoded ASCII as aUint8List, skipping the intermediateString. - Non-throwing decoders β the
tryFrom<Name>decoders returnnullinstead of throwing aFormatExceptionon invalid input. - Constant-time compare β
constantTimeEquals(a, b)compares two byte lists without exiting early on the first mismatch, for verifying MACs and digests. - Low-level building blocks β the generic converters
BitEncoder/BitDecoder,ByteEncoder/ByteDecoder, andAlphabetEncoder/AlphabetDecoderare exported for building custom codecs.
ByteCollector is the byte container shared with hashlib, holding the output
of a hash or encoding function and re-encoding it on demand:
| Method / getter | Description |
|---|---|
bytes |
Raw bytes as Uint8List |
length |
Number of bytes |
buffer |
The backing ByteBuffer |
hex([upper]) |
Hexadecimal string (optionally uppercase) |
binary() / octal() |
Binary / octal string representation |
base32({upper, padding}) |
Base-32 encoding |
base64({urlSafe, padding}) |
Base-64 encoding |
bigInt({endian}) |
Interprets the bytes as a BigInt |
number([bitLength, endian]) |
Reads an unsigned integer of the given bit-length |
ascii() / utf8() |
Decodes bytes as ASCII / UTF-8 |
to(encoding) |
Decodes bytes with a given dart:convert Encoding |
isEqual(other) |
Constant-time compare against bytes, buffer, or hex text |
The same bytes, rendered in each supported Base-32 alphabet β useful for human-friendly identifiers (Crockford), URLs (z-base-32), or geospatial codes (geohash):
import 'package:convertlib/convertlib.dart';
void main() {
final data = toUtf8('Hello, convertlib!');
print('standard : ${toBase32(data)}');
print('lowercase : ${toBase32(data, lower: true)}');
print('no padding : ${toBase32(data, padding: false)}');
print('base32hex : ${toBase32(data, codec: Base32Codec.hex)}');
print('crockford : ${toBase32(data, codec: Base32Codec.crockford)}');
print('z-base-32 : ${toBase32(data, codec: Base32Codec.z)}');
print('geohash : ${toBase32(data, codec: Base32Codec.geohash)}');
print('word-safe : ${toBase32(data, codec: Base32Codec.wordSafe)}');
// Decoding is the exact inverse of encoding
final back = fromBase32(toBase32(data));
print('roundtrip : ${fromUtf8(back)}');
}For tokens embedded in URLs or JSON, drop the padding and switch to the URL/filename-safe alphabet:
import 'package:convertlib/convertlib.dart';
void main() {
final data = toUtf8('a >> b, c/d');
print('standard : ${toBase64(data)}');
print('url-safe : ${toBase64(data, url: true)}');
print('no padding : ${toBase64(data, url: true, padding: false)}');
print('bcrypt : ${toBase64(data, codec: Base64Codec.bcrypt)}');
// Decode back to the original bytes
final back = fromBase64(toBase64(data, url: true));
print('roundtrip : ${fromUtf8(back)}');
}Read a byte sequence as an arbitrary-precision integer and back. Combine with
BigInt.toRadixString for a decimal (or any-base) string representation:
import 'package:convertlib/convertlib.dart';
void main() {
var input = [0x3, 0xF1];
print("input => $input");
var encoded = toBigInt(input).toRadixString(10);
print("to decimal => $encoded");
var decoded = fromBigInt(BigInt.parse(encoded, radix: 10));
print("from decimal => $decoded");
}Build and parse password-hash strings such as
$argon2id$v=19$m=65536,t=3,p=4$...$.... The builder Base-64 encodes salt and
hash bytes for you, and fromCrypt gives the fields back as typed accessors:
import 'package:convertlib/convertlib.dart';
void main() {
// Build a PHC / Modular Crypt Format string from its parts.
// `saltBytes` and `hashBytes` are Base-64 encoded (no padding) for you.
final data = CryptData.builder('argon2id')
.version('19')
.param('m', 65536)
.param('t', 3)
.param('p', 4)
.saltBytes(toUtf8('a-16-byte-salt!!'))
.hashBytes(List.generate(32, (i) => i))
.build();
final encoded = toCrypt(data);
print('encoded : $encoded');
// Parse the string back into its structured fields.
final parsed = fromCrypt(encoded);
print('id : ${parsed.id}');
print('version : ${parsed.versionInt()}');
print('m,t,p : ${parsed.getIntParam('m')}, '
'${parsed.getIntParam('t')}, ${parsed.getIntParam('p')}');
print('salt : ${fromUtf8(parsed.saltBytes()!)}');
print('hash : ${toHex(parsed.hashBytes()!)}');
}Codecs are trivial to get subtly wrong. It is not enough to check just the
decode(encode(x)) == x round-trips alone. For example: a dropped bit mask
can produce output that still passes round-trips cleanly. This package is
tested against that failure mode directly:
- Expectations come from outside the code. Every codec is checked against
official vectors (RFC 4648 "foobar", RFC 3629 UTF-8 boundaries, PHC / bcrypt
strings) and differentially against independent reference implementations:
dart:convert,base_codecs, andbase32, so a consistently-wrong codec cannot hide behind a passing round-trip. - Wide input coverage.
- Randomized round-trips at every length from 0 to 99 for each alphabet variant
- Full-range Unicode code points and surrogate-pair handling for UTF-8
- Per-instance assertions of the exact alphabet string each codec emits, which catch miscopied lookup tables.
- Failure paths are asserted, not assumed. Malformed UTF-8, invalid characters, and exhaustive invalid-length sweeps all verify that a typed error is raised (with the exact message and offset), never a silent wrong result.
- Every platform. The suite runs on the Dart VM, Node.js (JavaScript), and Chrome (WASM) in CI across Dart SDK 2.19 and stable on Linux, macOS, and Windows. Special care has been given for the web, where integers behave differently.
- Regressions stay fixed. Real bugs found in the past (a word-safe Base-32
table accidentally copied from z-base-32, a dropped
& 0x3Fmask in the UTF-8 decoder, aByteCollector.isEqualself-comparison) each have a permanent, externally-anchored regression test, and a differential fuzz harness re-checks the codecs against the reference implementations.
Because correctness is anchored to independent references rather than the
package's own agreement with itself, convertlib is safe to rely on in
production. If you do hit a discrepancy, please open an issue and include
your input and the expected output.
- Convertlib : https://pub.dev/packages/convertlib
- Base Codecs : https://pub.dev/packages/base_codecs
- Base32 : https://pub.dev/packages/base32
- Dart Convert : https://api.dart.dev/stable/dart-convert/dart-convert-library.html
UTF-8 throughput is measured per source code point, not per byte.
| Codec | Library | 1MB message | 1KB message | 32B message |
|---|---|---|---|---|
| Base-2 | convertlib | ββββββββββββββββ 1.86 Gbps π |
ββββββββββββββββ 2.11 Gbps π |
ββββββββββββββββ 1.94 Gbps π |
| Base-8 | convertlib | ββββββββββββββββ 4.48 Gbps π |
ββββββββββββββββ 5.06 Gbps π |
ββββββββββββββββ 4.4 Gbps π |
| Base-16 | convertlib | ββββββββββββββββ 4.98 Gbps π |
ββββββββββββββββ 5.59 Gbps π |
ββββββββββββββββ 4.51 Gbps π |
| base_codecs | ββββββββββββββββ 284 Mbps π»17.55x |
ββββββββββββββββ 247 Mbps π»22.59x |
ββββββββββββββββ 247 Mbps π»18.24x |
|
| Base-32 | convertlib | ββββββββββββββββ 5.44 Gbps π |
ββββββββββββββββ 5.99 Gbps π |
ββββββββββββββββ 4.53 Gbps π |
| base_codecs | ββββββββββββββββ 577 Mbps π»9.43x |
ββββββββββββββββ 421 Mbps π»14.23x |
ββββββββββββββββ 387 Mbps π»11.7x |
|
| base32 | ββββββββββββββββ 631 Kbps π»8618.49x |
ββββββββββββββββ 132 Mbps π»45.35x |
ββββββββββββββββ 137 Mbps π»33.02x |
|
| Base-64 | convertlib | ββββββββββββββββ 6.04 Gbps π |
ββββββββββββββββ 6.68 Gbps π |
ββββββββββββββββ 5.48 Gbps π |
| dart:convert | ββββββββββββββββ 5.61 Gbps π»1.08x |
ββββββββββββββββ 6.11 Gbps π»1.09x |
ββββββββββββββββ 4.36 Gbps π»1.26x |
|
| UTF-8 | convertlib | ββββββββββββββββ 3.14 Gbps π |
ββββββββββββββββ 3.4 Gbps π |
ββββββββββββββββ 2.78 Gbps π |
| dart:convert | ββββββββββββββββ 2.6 Gbps π»1.21x |
ββββββββββββββββ 2.73 Gbps π»1.25x |
ββββββββββββββββ 2.22 Gbps π»1.25x |
| Codec | Library | 1MB message | 1KB message | 32B message |
|---|---|---|---|---|
| Base-2 | convertlib | ββββββββββββββββ 1.74 Gbps π |
ββββββββββββββββ 1.76 Gbps π |
ββββββββββββββββ 1.64 Gbps π |
| Base-8 | convertlib | ββββββββββββββββ 4.15 Gbps π |
ββββββββββββββββ 4.22 Gbps π |
ββββββββββββββββ 3.68 Gbps π |
| Base-16 | convertlib | ββββββββββββββββ 3.65 Gbps π |
ββββββββββββββββ 3.71 Gbps π |
ββββββββββββββββ 3.26 Gbps π |
| base_codecs | ββββββββββββββββ 406 Mbps π»8.98x |
ββββββββββββββββ 410 Mbps π»9.04x |
ββββββββββββββββ 396 Mbps π»8.24x |
|
| Base-32 | convertlib | ββββββββββββββββ 4.03 Gbps π |
ββββββββββββββββ 4.09 Gbps π |
ββββββββββββββββ 3.38 Gbps π |
| base_codecs | ββββββββββββββββ 266 Mbps π»15.13x |
ββββββββββββββββ 267 Mbps π»15.33x |
ββββββββββββββββ 239 Mbps π»14.13x |
|
| base32 | ββββββββββββββββ 176 Mbps π»22.84x |
ββββββββββββββββ 199 Mbps π»20.62x |
ββββββββββββββββ 153 Mbps π»22.14x |
|
| Base-64 | convertlib | ββββββββββββββββ 4.98 Gbps π |
ββββββββββββββββ 5.16 Gbps π |
ββββββββββββββββ 4.32 Gbps π |
| dart:convert | ββββββββββββββββ 3.5 Gbps π»1.42x |
ββββββββββββββββ 3.6 Gbps π»1.43x |
ββββββββββββββββ 2.87 Gbps π»1.51x |
|
| UTF-8 | convertlib | ββββββββββββββββ 1.65 Gbps |
ββββββββββββββββ 1.68 Gbps |
ββββββββββββββββ 1.28 Gbps |
| dart:convert | ββββββββββββββββ 1.75 Gbps πΊ1.06x |
ββββββββββββββββ 1.81 Gbps πΊ1.08x |
ββββββββββββββββ 1.72 Gbps πΊ1.35x |
| Codec | Library | 4KB message | 256B message | 32B message |
|---|---|---|---|---|
| bytes β BigInt | convertlib | ββββββββββββββββ 119 Mbps π |
ββββββββββββββββ 115 Mbps π |
ββββββββββββββββ 98.21 Mbps π |
| BigInt β bytes | convertlib | ββββββββββββββββ 256 Mbps π |
ββββββββββββββββ 250 Mbps π |
ββββββββββββββββ 223 Mbps π |
All benchmarks are done on 36GB Apple M3 Pro using compiled exe
Dart SDK version: 3.12.2 (stable) (Tue Jun 9 01:11:39 2026 -0700) on "macos_arm64"
BSD 3-Clause License. See the LICENSE file for details. Issues and contributions are welcome at github.com/bitanon/convertlib.