Base58 Class
Definition
Provides Base58 encoding and decoding of binary data using the Bitcoin/Flickr or Ripple alphabets.
public static class Base58
- Inheritance
-
Base58
- Inherited Members
Examples
// Bitcoin/Flickr Base58 (default) - leading zero bytes encode as leading '1' characters.
byte[] data = { 0x00, 0xDE, 0xAD, 0xBE, 0xEF };
string encoded = Base58.Encode(data);
// Ripple alphabet - a permutation used by the XRP ledger.
string ripple = Base58.Encode(data, Base58Variant.Ripple);
// Round-trip.
byte[] roundtrip = Base58.Decode(encoded);
Remarks
Base58 is a non-power-of-two radix (radix 58) used by Bitcoin addresses, IPFS CIDs, Solana, and similar protocols. Because the radix is not a power of two, encoding is performed using big-integer arithmetic rather than the bit-stream technique used by Base16, Base32, and Base64.
Leading zero bytes in the input are encoded as leading alphabet[0] characters (typically 1 in the
Bitcoin/Flickr alphabet) so that the byte-level and character-level forms preserve a meaningful prefix.
Base58 has no padding character and no standard decorations. The UpperCase, IncludePrefix, InsertSpacing, InsertLineBreaks, and OmitPadding flags are no-ops. The AllowPrefix and AllowMissingPadding flags are likewise ignored; only IgnoreWhitespace has an effect on decode.
Methods
Decode(char[], int, int, Base58Variant, BaseFormatStyles)
Decodes a portion of a character array into a byte array.
public static byte[] Decode(char[] chars, int offset, int count, Base58Variant variant = Base58Variant.BitcoinFlickr, BaseFormatStyles style = BaseFormatStyles.None)
Parameters
charschar[]The character array.
offsetintThe starting offset.
countintThe number of characters.
variantBase58VariantThe Base58 variant.
styleBaseFormatStylesParsing styles.
Returns
- byte[]
The decoded byte array.
Exceptions
- ArgumentNullException
Thrown when
charsis null.- ArgumentOutOfRangeException
Thrown when
offset,count, orvariantis out of range.- ArgumentException
Thrown when the segment defined by
offsetandcountexceeds the available range ofchars.- FormatException
Thrown when the input is not valid Base58.
Decode(ReadOnlySpan<char>, Base58Variant, BaseFormatStyles)
Decodes a Base58 character span into a byte array.
public static byte[] Decode(ReadOnlySpan<char> chars, Base58Variant variant = Base58Variant.BitcoinFlickr, BaseFormatStyles style = BaseFormatStyles.None)
Parameters
charsReadOnlySpan<char>The Base58 character span.
variantBase58VariantThe Base58 variant.
styleBaseFormatStylesParsing styles.
Returns
- byte[]
The decoded byte array. Returns Empty<T>() for empty input.
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.- FormatException
Thrown when the input is not valid Base58.
Decode(string, Base58Variant, BaseFormatStyles)
Decodes a Base58 string into a byte array using the supplied variant.
public static byte[] Decode(string s, Base58Variant variant = Base58Variant.BitcoinFlickr, BaseFormatStyles style = BaseFormatStyles.None)
Parameters
sstringThe Base58 input.
variantBase58VariantThe Base58 variant.
styleBaseFormatStylesParsing styles. Only IgnoreWhitespace has effect.
Returns
- byte[]
The decoded byte array.
Exceptions
- ArgumentNullException
Thrown when
sis null.- ArgumentOutOfRangeException
Thrown when
variantis undefined.- FormatException
Thrown when the input contains characters outside the variant alphabet.
DecodeFromUtf8(ReadOnlySpan<byte>, Span<byte>, out int, out int, Base58Variant, BaseFormatStyles)
Decodes UTF-8 Base58 bytes into a byte span using the OperationStatus return convention.
public static OperationStatus DecodeFromUtf8(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out int bytesWritten, Base58Variant variant = Base58Variant.BitcoinFlickr, BaseFormatStyles styles = BaseFormatStyles.None)
Parameters
sourceReadOnlySpan<byte>The UTF-8 Base58 source.
destinationSpan<byte>The destination byte span.
bytesConsumedintWhen this method returns, contains the number of source bytes consumed.
bytesWrittenintWhen this method returns, contains the number of bytes written.
variantBase58VariantThe variant.
stylesBaseFormatStylesParsing styles.
Returns
- OperationStatus
An OperationStatus describing the outcome.
Remarks
Base58 is not a streamable encoding; this overload always treats source as a complete input.
NeedMoreData is never returned.
DecodeGuid(ReadOnlySpan<char>, Base58Variant, BaseFormatStyles)
Decodes a Base58 representation of a Guid.
public static Guid DecodeGuid(ReadOnlySpan<char> source, Base58Variant variant = Base58Variant.BitcoinFlickr, BaseFormatStyles styles = BaseFormatStyles.None)
Parameters
sourceReadOnlySpan<char>The Base58 characters.
variantBase58VariantThe Base58 variant.
stylesBaseFormatStylesParsing styles.
Returns
Exceptions
- FormatException
Thrown when the input does not decode to exactly 16 bytes.
Encode(byte[], Base58Variant)
Encodes bytes into a Base58 string using the supplied variant.
public static string Encode(byte[] bytes, Base58Variant variant = Base58Variant.BitcoinFlickr)
Parameters
bytesbyte[]The bytes to encode.
variantBase58VariantThe Base58 variant.
Returns
- string
A Base58 string.
Exceptions
- ArgumentNullException
Thrown when
bytesis null.- ArgumentOutOfRangeException
Thrown when
variantis undefined.
Encode(byte[], int, int, Base58Variant)
Encodes a portion of bytes into a Base58 string.
public static string Encode(byte[] bytes, int offset, int count, Base58Variant variant = Base58Variant.BitcoinFlickr)
Parameters
bytesbyte[]The byte array to encode.
offsetintThe starting offset.
countintThe number of bytes to encode.
variantBase58VariantThe Base58 variant.
Returns
- string
A Base58 string.
Exceptions
- ArgumentNullException
Thrown when
bytesis null.- ArgumentOutOfRangeException
Thrown when
offset,count, orvariantis out of range.- ArgumentException
Thrown when the segment defined by
offsetandcountexceeds the available range ofbytes.
Encode(Guid, Base58Variant)
Encodes the byte representation of value as a Base58 string. Typical output is 22 characters
(radix-58 representation of 16 bytes), though leading zero bytes inflate the count slightly.
public static string Encode(Guid value, Base58Variant variant = Base58Variant.BitcoinFlickr)
Parameters
valueGuidThe Guid to encode.
variantBase58VariantThe Base58 variant.
Returns
- string
A Base58 string of the GUID bytes (mixed-endian, matching TryWriteBytes(Span<byte>)).
Encode(ReadOnlySpan<byte>, Base58Variant)
Encodes a span of bytes into a Base58 string.
public static string Encode(ReadOnlySpan<byte> bytes, Base58Variant variant = Base58Variant.BitcoinFlickr)
Parameters
bytesReadOnlySpan<byte>The bytes to encode.
variantBase58VariantThe Base58 variant.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.
Encode(ReadOnlySpan<byte>, IBufferWriter<char>, Base58Variant)
Encodes source as Base58 characters into writer, suitable for use in
pipelines and other IBufferWriter<T>-based scenarios.
public static int Encode(ReadOnlySpan<byte> source, IBufferWriter<char> writer, Base58Variant variant = Base58Variant.BitcoinFlickr)
Parameters
sourceReadOnlySpan<byte>The bytes to encode.
writerIBufferWriter<char>The buffer writer that receives the encoded characters.
variantBase58VariantThe Base58 variant.
Returns
- int
The number of characters written.
Exceptions
- ArgumentNullException
Thrown when
writeris null.- ArgumentOutOfRangeException
Thrown when
variantis undefined.
Encode(ReadOnlySpan<byte>, Span<char>, Base58Variant)
Encodes a span of bytes directly into a destination character span.
public static int Encode(ReadOnlySpan<byte> bytes, Span<char> destination, Base58Variant variant = Base58Variant.BitcoinFlickr)
Parameters
bytesReadOnlySpan<byte>The bytes to encode.
destinationSpan<char>The destination span. Must be at least GetMaxEncodedLength(int) characters in size for safe sizing.
variantBase58VariantThe Base58 variant.
Returns
- int
The number of characters written.
Exceptions
- ArgumentException
Thrown when
destinationis too small.- ArgumentOutOfRangeException
Thrown when
variantis undefined.
EncodeToUtf8(ReadOnlySpan<byte>, Base58Variant)
Encodes source into a UTF-8 Base58 byte array using the supplied variant.
public static byte[] EncodeToUtf8(ReadOnlySpan<byte> source, Base58Variant variant = Base58Variant.BitcoinFlickr)
Parameters
sourceReadOnlySpan<byte>The bytes to encode.
variantBase58VariantThe Base58 variant.
Returns
- byte[]
The UTF-8 encoded Base58 bytes.
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.
EncodeToUtf8(ReadOnlySpan<byte>, IBufferWriter<byte>, Base58Variant)
Encodes source as UTF-8 Base58 bytes into writer, suitable for use in
pipelines and other IBufferWriter<T>-based scenarios.
public static int EncodeToUtf8(ReadOnlySpan<byte> source, IBufferWriter<byte> writer, Base58Variant variant = Base58Variant.BitcoinFlickr)
Parameters
sourceReadOnlySpan<byte>The bytes to encode.
writerIBufferWriter<byte>The buffer writer that receives the UTF-8 bytes.
variantBase58VariantThe Base58 variant.
Returns
- int
The number of UTF-8 bytes written.
Exceptions
- ArgumentNullException
Thrown when
writeris null.- ArgumentOutOfRangeException
Thrown when
variantis undefined.
FromBase58String(ReadOnlySpan<byte>)
Decodes UTF-8 Base58 bytes into a byte array using the Bitcoin/Flickr alphabet.
public static byte[] FromBase58String(ReadOnlySpan<byte> utf8Source)
Parameters
utf8SourceReadOnlySpan<byte>The UTF-8 Base58 source.
Returns
- byte[]
The decoded byte array.
Exceptions
- FormatException
Thrown when the input is not valid Bitcoin/Flickr Base58.
FromBase58String(ReadOnlySpan<byte>, Span<byte>, out int, out int)
Strict Bitcoin/Flickr Base58 decode from a UTF-8 byte span using the OperationStatus return convention.
public static OperationStatus FromBase58String(ReadOnlySpan<byte> utf8Source, Span<byte> destination, out int bytesConsumed, out int bytesWritten)
Parameters
utf8SourceReadOnlySpan<byte>The UTF-8 Base58 source.
destinationSpan<byte>The destination byte span.
bytesConsumedintWhen this method returns, contains the number of source bytes consumed.
bytesWrittenintWhen this method returns, contains the number of bytes written.
Returns
- OperationStatus
An OperationStatus describing the outcome.
FromBase58String(ReadOnlySpan<char>)
Decodes a Base58 character span using the Bitcoin/Flickr alphabet.
public static byte[] FromBase58String(ReadOnlySpan<char> chars)
Parameters
charsReadOnlySpan<char>The input span.
Returns
- byte[]
The decoded byte array.
Exceptions
- FormatException
Thrown when the input is not valid Bitcoin/Flickr Base58.
FromBase58String(ReadOnlySpan<char>, Span<byte>, out int, out int)
Strict Bitcoin/Flickr Base58 decode from a character span using the OperationStatus return convention.
public static OperationStatus FromBase58String(ReadOnlySpan<char> source, Span<byte> destination, out int charsConsumed, out int bytesWritten)
Parameters
sourceReadOnlySpan<char>The character span.
destinationSpan<byte>The destination byte span.
charsConsumedintWhen this method returns, contains the number of characters consumed.
bytesWrittenintWhen this method returns, contains the number of bytes written.
Returns
- OperationStatus
An OperationStatus describing the outcome.
Remarks
Base58 is not a streamable encoding: isFinalBlock = false would have no useful effect, so the decoder
always treats the input as complete and never returns NeedMoreData.
FromBase58String(string)
Decodes s as Bitcoin/Flickr Base58 into a byte array.
public static byte[] FromBase58String(string s)
Parameters
sstringThe input.
Returns
- byte[]
The decoded byte array.
Exceptions
- ArgumentNullException
Thrown when
sis null.- FormatException
Thrown when the input is not valid Bitcoin/Flickr Base58.
GetMaxDecodedLength(int)
Returns an upper bound on the number of bytes that decoding charCount characters can
produce.
public static int GetMaxDecodedLength(int charCount)
Parameters
charCountintThe input character count.
Returns
- int
An upper bound on the decoded byte count.
Exceptions
- ArgumentOutOfRangeException
Thrown when
charCountis negative.
GetMaxEncodedLength(int)
Returns an upper bound on the number of characters required to encode byteCount bytes.
public static int GetMaxEncodedLength(int byteCount)
Parameters
byteCountintThe input byte count.
Returns
- int
An upper bound on the encoded character count.
Remarks
Because Base58 is a non-power-of-two radix and the exact encoded length depends on the leading-zero count of the input data, this overload returns a worst-case upper bound suitable for buffer sizing. The actual output length is the result of Encode(ReadOnlySpan<byte>, Base58Variant) on the specific data.
Exceptions
- ArgumentOutOfRangeException
Thrown when
byteCountis negative.
IsBase58Digit(char, Base58Variant)
Indicates whether value is a valid digit for the supplied Base58 variant.
public static bool IsBase58Digit(char value, Base58Variant variant = Base58Variant.BitcoinFlickr)
Parameters
valuecharThe character to test.
variantBase58VariantThe variant.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.
IsValid(ReadOnlySpan<char>, Base58Variant, BaseFormatStyles)
Indicates whether source is a valid Base58 input under the supplied variant.
public static bool IsValid(ReadOnlySpan<char> source, Base58Variant variant = Base58Variant.BitcoinFlickr, BaseFormatStyles styles = BaseFormatStyles.None)
Parameters
sourceReadOnlySpan<char>The character span.
variantBase58VariantThe variant.
stylesBaseFormatStylesParsing styles. Only IgnoreWhitespace has effect.
Returns
- bool
true when the input is no longer than Bodu.Text.Encoding.Base58.MaxDecodeInputLength and every retained character is in the variant alphabet; otherwise false.
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.
ToBase58String(byte[])
Encodes inArray into a Base58 string using the Bitcoin/Flickr alphabet.
public static string ToBase58String(byte[] inArray)
Parameters
inArraybyte[]The byte array to encode.
Returns
- string
A Base58 (Bitcoin/Flickr) string.
Exceptions
- ArgumentNullException
Thrown when
inArrayis null.
ToBase58String(byte[], int, int)
Encodes a portion of inArray into a Base58 string using the Bitcoin/Flickr alphabet.
public static string ToBase58String(byte[] inArray, int offset, int length)
Parameters
inArraybyte[]The byte array.
offsetintThe starting offset.
lengthintThe number of bytes to encode.
Returns
- string
A Base58 string.
Exceptions
- ArgumentNullException
Thrown when
inArrayis null.- ArgumentOutOfRangeException
Thrown when
offsetorlengthis out of range.- ArgumentException
Thrown when the segment defined by
offsetandlengthexceeds the available range ofinArray.
ToBase58String(ReadOnlySpan<byte>)
Encodes bytes into a Base58 string using the Bitcoin/Flickr alphabet.
public static string ToBase58String(ReadOnlySpan<byte> bytes)
Parameters
bytesReadOnlySpan<byte>The bytes.
Returns
- string
A Base58 string.
TryDecode(ReadOnlySpan<char>, Span<byte>, out int, Base58Variant, BaseFormatStyles)
Attempts to decode a Base58 character span into a destination byte span.
public static bool TryDecode(ReadOnlySpan<char> chars, Span<byte> destination, out int bytesWritten, Base58Variant variant = Base58Variant.BitcoinFlickr, BaseFormatStyles style = BaseFormatStyles.None)
Parameters
charsReadOnlySpan<char>The Base58 character span.
destinationSpan<byte>The destination byte span.
bytesWrittenintWhen this method returns, contains the number of bytes written, or
0on failure.variantBase58VariantThe Base58 variant.
styleBaseFormatStylesParsing styles.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.
TryDecodeGuid(ReadOnlySpan<char>, out Guid, Base58Variant, BaseFormatStyles)
Attempts to decode a Base58 representation of a Guid.
public static bool TryDecodeGuid(ReadOnlySpan<char> source, out Guid value, Base58Variant variant = Base58Variant.BitcoinFlickr, BaseFormatStyles styles = BaseFormatStyles.None)
Parameters
sourceReadOnlySpan<char>The Base58 characters.
valueGuidWhen this method returns, contains the decoded Guid or Empty.
variantBase58VariantThe Base58 variant.
stylesBaseFormatStylesParsing styles.
Returns
TryEncode(ReadOnlySpan<byte>, Span<char>, out int, Base58Variant)
Attempts to encode a span of bytes into a destination character span.
public static bool TryEncode(ReadOnlySpan<byte> bytes, Span<char> destination, out int charsWritten, Base58Variant variant = Base58Variant.BitcoinFlickr)
Parameters
bytesReadOnlySpan<byte>The bytes to encode.
destinationSpan<char>The destination span.
charsWrittenintWhen this method returns, contains the number of characters written.
variantBase58VariantThe Base58 variant.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.
TryEncodeToUtf8(ReadOnlySpan<byte>, Span<byte>, out int, Base58Variant)
Attempts to encode source as UTF-8 Base58 bytes into destination.
public static bool TryEncodeToUtf8(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, Base58Variant variant = Base58Variant.BitcoinFlickr)
Parameters
sourceReadOnlySpan<byte>The bytes to encode.
destinationSpan<byte>The destination UTF-8 byte span.
bytesWrittenintWhen this method returns, contains the number of bytes written.
variantBase58VariantThe Base58 variant.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.
TryToBase58String(ReadOnlySpan<byte>, Span<byte>, out int)
Attempts to encode source as a Bitcoin/Flickr Base58 UTF-8 byte span.
public static bool TryToBase58String(ReadOnlySpan<byte> source, Span<byte> utf8Destination, out int bytesWritten)
Parameters
sourceReadOnlySpan<byte>The bytes to encode.
utf8DestinationSpan<byte>The UTF-8 destination span.
bytesWrittenintWhen this method returns, contains the number of bytes written.
Returns
TryToBase58String(ReadOnlySpan<byte>, Span<char>, out int)
Attempts to encode source as a Bitcoin/Flickr Base58 character span.
public static bool TryToBase58String(ReadOnlySpan<byte> source, Span<char> destination, out int charsWritten)
Parameters
sourceReadOnlySpan<byte>The bytes to encode.
destinationSpan<char>The destination character span.
charsWrittenintWhen this method returns, contains the number of characters written.
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |