Codecs
convertlib
Fast, error-resilient, zero-dependency codecs for Dart.
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.
Highlights
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.
Quickstart
Install it, import it, and you are ready. This example comes straight from the README on the repository’s default branch, so it never drifts from the docs.
More recipes in the guideimport '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'}');
}Performance
Throughput against base_codecs, base32, and dart:convert at a typical input size (shown on each card). Higher is better.
21/24
head-to-head benchmarks won (88%)
- convertlib5.59 Gbps
- base_codecs247 Mbps
23× the fastest alternative
- convertlib3.71 Gbps
- base_codecs410 Mbps
9.0× the fastest alternative
- convertlib5.99 Gbps
- base_codecs421 Mbps
- base32132 Mbps
14× the fastest alternative
- convertlib4.09 Gbps
- base_codecs267 Mbps
- base32199 Mbps
15× the fastest alternative
- convertlib6.68 Gbps
- dart:convert6.11 Gbps
1.09× the fastest alternative
- convertlib5.16 Gbps
- dart:convert3.6 Gbps
1.43× the fastest alternative
All benchmarks are done on 36GB Apple M3 Pro using compiled exe, Dart 3.12.2.Explore all benchmarks →
Supported codecs
| 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 |
🔤 Alphabet variants
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)
🧰 Bytes, non-throwing decode, and constant-time compare
- 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
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 |
What's new in 3.6.1
Changelog →- Update
README.mdfor the 3.6.0 API: theto<Name>Bytesbyte encoders, the non-throwingtryFrom<Name>decoders,constantTimeEquals, the exported converter base classes, and the case-insensitive Crockford decoding.
3.6.13.6.03.5.13.5.03.4.03.3.1