Skip to content

Repository files navigation

SimpleBase

NuGet VersionBuild Status

This is my own take for exotic base encodings like Base32, Base58 and Base85. I started to write it in 2013 as coding practice and kept it as a small pet project. I suggest anyone who wants to brush up their coding skills to give those encoding problems a shot. They turned out to be more challenging than I expected. To grasp the algorithms I had to get a pen and paper to see how the math worked.

Features

  • Multibase: All formats covered by SimpleBase including a few Base64 variants are supported. Base2, Base8, and Base10 are also supported.
  • Base32: RFC 4648, BECH32, Crockford, z-base-32, Geohash, FileCoin and Extended Hex (BASE32-HEX) flavors with Crockford character substitution, or any other custom flavors.
  • Base36: Both lowercase and uppercase alphabets are supported.
  • Base45: RFC 9285 is supported.
  • Base58: All the standard (Bitcoin (BTC), Ripple (XRP), Monero (XMR)) and custom Base58 encoding methods are supported. Also provides Base58Check and Avalanche (AVAX) CB58 encoding/decoding helpers.
  • Base62: The standard Base62 encoding/decoding supported along with a custom alphabet.
  • Base85: Ascii85, Z85 and custom flavors. IPv6 encoding/decoding support.
  • Base16: UpperCase, LowerCase and ModHex flavors. An experimental hexadecimal encoder/decoder just to see how far I can take the optimizations compared to .NET's implementations. It's quite fast now, but .NET has Convert.FromHexString() method since .NET 5. This is mostly a baseline implementation now (except for when you need ModHex).
  • Base256 Emoji🚀: Supported by Multibase, and can also be used individually.
  • One-shot memory buffer based APIs for simple use cases.
  • Stream-based async APIs for more advanced scenarios.
  • Lightweight: No dependencies.
  • Support for big-endian CPUs like IBM s390x (zArchitecture).
  • Thread-safe.
  • Simple to use.

NuGet

To install it from NuGet:

Install-Package SimpleBase

If you need .NET Standard 2.0 compatible version for targeting older .NET Framework or .NET Core, use the 2.x release line instead. It's missing newer features, but still supported:

Install-Package SimpleBase -MaximumVersion 2.999.0

Usage

The basic usage for encoding a buffer into, say, Base32, is as simple as:

usingSimpleBase;byte[]myBuffer;stringresult=Base32.Crockford.Encode(myBuffer,padding:true);// you can also use "ExtendedHex" or "Rfc4648" as encoder flavors

Decoding is also similar:

usingSimpleBase;stringmyText= ...byte[]result=Base32.Crockford.Decode(myText);

See the wiki for more types of examples and full documentation.

Benchmark Results

Small buffer sizes are used (64 characters). They are closer to real life applications. Base58 performs really bad in decoding of larger buffer sizes, due to polynomial complexity of numeric base conversions.

BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.8894/25H2/2025Update/HudsonValley2) AMD Ryzen 9 5950X 3.40GHz, 1 CPU, 32 logical and 16 physical cores .NET SDK 10.0.302 [Host] : .NET 10.0.10 (10.0.10, 10.0.1026.32716), X64 RyuJIT x86-64-v3 DefaultJob : .NET 10.0.10 (10.0.10, 10.0.1026.32716), X64 RyuJIT x86-64-v3

MethodMeanErrorStdDevGen0Allocated
DotNet_Base6417.69 ns0.117 ns0.104 ns0.0120200 B
Base2_Default229.76 ns4.648 ns8.843 ns0.06251048 B
Base8_Default110.19 ns2.138 ns2.927 ns0.0219368 B
Base10_Default10,380.62 ns43.676 ns36.471 ns0.0153328 B
Base16_UpperCase68.47 ns1.209 ns1.131 ns0.0167280 B
Multibase_Base16_UpperCase81.15 ns1.625 ns3.734 ns0.0334560 B
Base32_CrockfordWithPadding128.36 ns1.676 ns1.308 ns0.0138232 B
Base36_LowerCase6,638.64 ns25.361 ns23.722 ns0.0076224 B
Base45_Default97.34 ns1.872 ns1.659 ns0.0129216 B
Base58_Bitcoin5,849.35 ns27.707 ns23.137 ns0.0076200 B
Base58_Monero174.22 ns3.455 ns4.613 ns0.0119200 B
Base62_Default5,872.56 ns98.857 ns92.471 ns0.0076192 B
Base85_Z85119.07 ns2.199 ns2.258 ns0.0110184 B
Base256Emoji_Default160.29 ns3.073 ns3.156 ns0.0162272 B

Decoding (80 character string, except Base45 which must use an 81 character string)

MethodMeanErrorStdDevGen0Gen1Allocated
DotNet_Base6473.50 ns0.586 ns0.548 ns0.0052-88 B
Base2_Default75.73 ns0.604 ns0.535 ns0.0024-40 B
Base8_Default48.33 ns0.564 ns0.441 ns0.0033-56 B
Base10_Default1,508.23 ns14.977 ns14.009 ns0.0038-64 B
Base16_UpperCase39.44 ns0.346 ns0.324 ns0.0038-64 B
Base16_UpperCase_TextReader208.16 ns4.168 ns8.130 ns0.49660.01558312 B
Multibase_Base16_UpperCase41.37 ns0.832 ns0.779 ns0.0038-64 B
Multibase_TryDecode_Base16_UpperCase36.23 ns0.506 ns0.395 ns---
Base32_Crockford77.25 ns0.380 ns0.355 ns0.0048-80 B
Base36_LowerCase2,235.53 ns24.883 ns23.276 ns0.0038-80 B
Base45_Default51.74 ns0.330 ns0.293 ns0.0048-80 B
Base58_Bitcoin2,729.74 ns15.788 ns13.996 ns0.0038-88 B
Base58_Monero57.42 ns0.658 ns0.513 ns0.0052-88 B
Base62_Default3,002.94 ns11.360 ns9.486 ns0.0038-88 B
Base85_Z85189.11 ns1.445 ns1.352 ns0.0052-88 B
Base256Emoji_Default223.56 ns2.182 ns1.934 ns0.0062-104 B

Notes

I'm sure there are areas for improvement. I didn't want to go further in optimizations which would hurt readability and extensibility. I might experiment on them in the future.

Test suite for Base32 isn't complete, I took most of it from RFC4648. Base58 really lacks a good spec or test vectors needed. I had to resort to using online converters to generate preliminary test vectors.

Base85 tests are also makseshift tests based on what output Cryptii produces. Contribution to missing test cases are greatly appreciated.

It's interesting that I wasn't able to reach .NET Base64's performance with Base16 with a straightforward managed code despite that it's much simpler. I was only able to match it after I converted Base16 to unsafe code with good independent interleaving so CPU pipeline optimizations could take place. Still not satisfied though. Is .NET's Base64 implementation native? Perhaps.

Thanks

Thanks to all contributors who provided patches, and reported bugs.

Chatting about this pet project with my friends @detaybey, @vhallac, @alkimake and @Utopians at one of our friend's birthday encouraged me to finish this. Thanks guys.

About

.NET library for encoding/decoding Base16, Base32, Base45, Base58, Base62, Base85, and Multibase.

Topics

Resources

Security policy

Stars

179 stars

Watchers

4 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages