SimpleBase

repository·main·Indexed 41 days ago

https://github.com/ssg/simplebase

A lightweight, dependency-free C# library for encoding and decoding data into various exotic base formats. It supports Multibase, Base2, Base8, Base10, Base16, Base32 (including Crockford, Bech32, and RFC 4648), Base36, Base45, Base58 (Bitcoin, Ripple, Monero), Base62, Base85, and Base256 Emoji. The library provides thread-safe, non-allocating APIs using Span<T>, as well as stream-based async APIs for .NET and .NET Standard 2.0.

Tokens
17.4K
Snippets
90
Records
113
Agent score
51%

What's inside SimpleBase

  1. Use Base32 encoding and decoding

    main

    The Base32 class in the SimpleBase namespace provides various methods to encode binary data or numbers into Base32 strings and decode them back. It supports multiple encoding flavors (alphabets) through static properties and allows for custom alphabets via constructors.

    Common tasks include:

    • Encoding byte spans or long/ulong numbers into strings.
    • Decoding Base32 strings back into byte arrays.
    • Stream-based encoding and decoding for large datasets.
    • Non-allocating operations using TryEncode and TryDecode with Span<T> for high performance.
    // Example: Encoding bytes to a string using RFC 4648
    byte[] data = { 0x01, 0x02, 0x03 };
    string encoded = Base32.Rfc4648.Encode(data);
    
    // Example: Decoding a string back to bytes
    byte[] decoded = Base32.Rfc4648.Decode(encoded);
  2. Supported encoding formats in SimpleBase

    main

    SimpleBase supports a wide variety of exotic and standard base encodings, including:

    • Multibase: Supports all formats covered by Multibase, including Base64 variants, as well as Base2, Base8, and Base10.
    • Base32: Includes RFC 4648, BECH32, Crockford, z-base-32, Geohash, FileCoin, and Extended Hex (BASE32-HEX) with Crockford substitution.
    • Base36: Supports both lowercase and uppercase alphabets.
    • Base45: Supports RFC 9285.
    • Base58: Supports standard flavors like Bitcoin (BTC), Ripple (XRP), and Monero (XMR), plus custom methods, Base58Check, and Avalanche (AVAX) CB58.
    • Base62: Standard Base62 and custom alphabet support.
    • Base85: Includes Ascii85, Z85, custom flavors, and IPv6 encoding/decoding.
    • Base16: UpperCase, LowerCase, and ModHex flavors.
    • Base256 Emoji🚀: Supported via Multibase or individually.

    SimpleBase is lightweight (no dependencies), thread-safe, and provides both one-shot memory buffer APIs and stream-based async APIs.

  3. Use Base256Emoji for emoji-based encoding

    main

    The Base256Emoji class in the SimpleBase namespace provides Base256 encoding using variable-length emojis. It implements IBaseCoder and INonAllocatingBaseCoder.

    Important Note: Because emojis can consist of one or more UTF-16 code points (chars), the encoded string length may not have a 1:1 relationship with the number of bytes. When processing encoded strings, be aware that a single emoji might occupy multiple char positions.

  4. Use Base85IPv6 for RFC 1924 encoding

    main

    The Base85IPv6 class provides a Base85 implementation specifically extended with functions to encode and decode IPv6 addresses using the RFC 1924 standard.

    Warning: The author notes that RFC 1924 is an early proposal that uses special characters (potentially causing security or UX issues), offers marginal length gains compared to standard IPv6 zero elimination, and is computationally slower due to its division-based algorithm. This implementation should primarily be used to support obscure standards or legacy code.

    // Example of initializing Base85IPv6 with an alphabet
    var coder = new Base85IPv6(Base85Alphabet.Rfc1924);
  5. Implement custom alphabets using CodingAlphabet

    main

    The CodingAlphabet abstract class serves as a basis for implementing custom alphabets for different encoding algorithms. If you want to implement your own encoding based on existing SimpleBase base classes, you should inherit from CodingAlphabet and define your specific character set.

    Key properties available to subclasses:

    • Length: The number of characters in the alphabet.
    • Value: The string representation of the alphabet characters.
    public abstract class CodingAlphabet : ICodingAlphabet
  6. Use predefined Base58 flavors (Bitcoin, Ripple, Flickr, Monero)

    main

    SimpleBase provides several static properties for common Base58 alphabets and algorithms used in different protocols:

    • Base58.Bitcoin: The Bitcoin flavor.
    • Base58.Ripple: The Ripple flavor.
    • Base58.Flickr: The Flickr flavor.
    • Base58.Monero: The Monero flavor (Note: This uses a different algorithm; see MoneroBase58 documentation).

    You can access these directly via the Base58 class to ensure compatibility with specific blockchain or protocol requirements.

    // Accessing specific protocol flavors
    var bitcoinCoder = Base58.Bitcoin;
    var rippleCoder = Base58.Ripple;
    var moneroCoder = Base58.Monero;
  7. How MoneroBase58 encoding works

    main
    MoneroBase58 is a variant of the Base58 encoding algorithm. Unlike standard Base58 implementations that process data byte-by-byte, MoneroBase58 encodes using 8-byte blocks and converts them into 11-byte blocks. This block-based approach reduces algorithmic complexity. If a block size is smaller than 11 bytes, the remaining space is padded with encoded zeroes (represented by the ZeroChar, which is "1" on the default Monero Base58 alphabet).
  8. Use Base16 encoding/decoding in SimpleBase

    main

    The Base16 class provides tools for encoding bytes into Base16 (hexadecimal) text and decoding Base16 text back into bytes. It supports different casing styles through static properties and allows for custom alphabets via its constructor.

    Decoding is case-insensitive across all variants.

    using SimpleBase;
    
    // Using predefined encoders
    var upper = Base16.UpperCase;
    var lower = Base16.LowerCase;
    var modHex = Base16.ModHex;
    
    // Encoding bytes to string
    string encoded = Base16.UpperCase.Encode(new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f });
    
    // Decoding string to bytes
    Span<byte> decoded = Base16.UpperCase.Decode("48656C6C6F");
  9. Use INonAllocatingBaseCoder for efficient encoding and decoding

    main
    The INonAllocatingBaseCoder interface in the SimpleBase namespace provides high-performance encoding and decoding functionality. It is designed to be non-allocating by requiring the caller to provide pre-allocated memory buffers (Span<T> or ReadOnlySpan<T>). This is ideal for performance-critical applications where minimizing garbage collection is important.
  10. Use DividingCoder<TAlphabet> for dividing encoding/decoding

    main

    The DividingCoder<TAlphabet> is an abstract base class in the SimpleBase namespace designed for implementing dividing encoding/decoding algorithms. It is intended to be inherited by specific dividing encoders. The length of the provided TAlphabet is used as the divisor for the encoding process.

    Note: This class is not used by Base58, as Base58 handles zero-prefixes differently than other dividing encodings.

    public abstract class DividingCoder<TAlphabet> : IBaseCoder, INonAllocatingBaseCoder
  11. Install SimpleBase via NuGet

    main

    To use SimpleBase in your .NET project, install it using the NuGet package manager.

    For modern .NET projects, use the latest version: Install-Package SimpleBase

    If you are targeting older environments that require .NET Standard 2.0 compatibility (such as older .NET Framework or .NET Core versions), use the 2.x release line: Install-Package SimpleBase -MaximumVersion 2.999.0

    Install-Package SimpleBase
  12. Encode and decode buffers using SimpleBase

    main

    SimpleBase provides a simple API for encoding byte buffers into various base encodings and decoding them back. Each encoding type (e.g., Base32, Base58) exposes specific 'flavors' (e.g., Crockford, Bitcoin) that implement different character sets or rules.

    Encoding

    To encode a byte[] into a string, use the .Encode() method on the desired encoding flavor. Some encodings support a padding option.

    Decoding

    To decode a string back into a byte[], use the .Decode() method on the corresponding encoding flavor.

    using SimpleBase;
    
    byte[] myBuffer = ...;
    // Encoding to Base32 Crockford with padding
    string result = Base32.Crockford.Encode(myBuffer, padding: true);
    
    // Decoding back to bytes
    string myText = result;
    byte[] decoded = Base32.Crockford.Decode(myText);