Repository navigation
OnixLabs.Core.Text.Base58Codec
Represents a codec for encoding and decoding Base-58 values.
| Namespace | OnixLabs.Core.Text |
| Assembly | OnixLabs.Core.dll (NuGet) |
| Inheritance |
object → Base58Codec
|
| Implements | IBaseCodec |
public sealed class Base58Codec : IBaseCodecBase58Codec implements the big-integer Base-58 algorithm, preserving leading zero bytes by emitting the alphabet's first character once per leading zero byte. The alphabet is taken from the supplied Base58FormatProvider; its default provider is Base58FormatProvider.Bitcoin.
Decoding fails if the input contains a character that is not part of the provider's alphabet (TryDecode returns false).
An empty input is treated as a successful no-op, producing an empty string (encoding) or an empty byte[] (decoding). Supplying a format provider of an incompatible type causes the Try* operations to return false and the Encode/Decode operations to throw a FormatException.
| Method | Returns | Description |
|---|---|---|
Encode(ReadOnlySpan<byte>, IFormatProvider?) |
string |
Encodes the specified value into a Base-58 string. |
Decode(ReadOnlySpan<char>, IFormatProvider?) |
byte[] |
Decodes the specified Base-58 value into a byte array. |
TryEncode(ReadOnlySpan<byte>, IFormatProvider?, out string) |
bool |
Tries to encode the specified value into a Base-58 string. |
TryDecode(ReadOnlySpan<char>, IFormatProvider?, out byte[]) |
bool |
Tries to decode the specified Base-58 value into a byte array. |
public string Encode(ReadOnlySpan<byte> value, IFormatProvider? provider = null);Encodes the specified value into a Base-58 string representation. See IBaseCodec.Encode for the full contract.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<byte> |
The value to encode into a Base-58 string representation. |
provider |
IFormatProvider? |
The Base58FormatProvider that supplies the alphabet. When null or a CultureInfo, defaults to Base58FormatProvider.Bitcoin. |
Returns — A new Base-58 string representation encoded from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value cannot be encoded, or provider is not a Base58FormatProvider. |
public byte[] Decode(ReadOnlySpan<char> value, IFormatProvider? provider = null);Decodes the specified Base-58 representation into a byte array. See IBaseCodec.Decode for the full contract.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The Base-58 value to decode into a byte array. |
provider |
IFormatProvider? |
The Base58FormatProvider that supplies the alphabet. When null or a CultureInfo, defaults to Base58FormatProvider.Bitcoin. |
Returns — A new byte array decoded from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value cannot be decoded, contains a character not in the provider's alphabet, or provider is not a Base58FormatProvider. |
public bool TryEncode(ReadOnlySpan<byte> value, IFormatProvider? provider, out string result);Tries to encode the specified value into a Base-58 string representation. See IBaseCodec.TryEncode for the full contract.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<byte> |
The value to encode into a Base-58 string representation. |
provider |
IFormatProvider? |
The Base58FormatProvider that supplies the alphabet. |
result |
out string |
When this method returns, contains the Base-58 string encoded from value, or an empty string if the value could not be encoded. |
Returns — true if the value was encoded successfully; otherwise, false.
public bool TryDecode(ReadOnlySpan<char> value, IFormatProvider? provider, out byte[] result);Tries to decode the specified Base-58 representation into a byte array. See IBaseCodec.TryDecode for the full contract.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The Base-58 value to decode into a byte array. |
provider |
IFormatProvider? |
The Base58FormatProvider that supplies the alphabet. |
result |
out byte[] |
When this method returns, contains the byte array decoded from value, or an empty byte array if the value could not be decoded. |
Returns — true if the value was decoded successfully; otherwise, false.
using System;
using System.Text;
using OnixLabs.Core.Text;
byte[] data = Encoding.UTF8.GetBytes("Hello, World!");
Base58Codec codec = new();
string encoded = codec.Encode(data); // uses Bitcoin alphabet by default
byte[] decoded = codec.Decode(encoded);
Console.WriteLine(Encoding.UTF8.GetString(decoded)); // Hello, World!using System;
using OnixLabs.Core.Text;
byte[] data = [0xDE, 0xAD, 0xBE, 0xEF];
Base58Codec codec = new();
string bitcoin = codec.Encode(data, Base58FormatProvider.Bitcoin);
string flickr = codec.Encode(data, Base58FormatProvider.Flickr);
string ripple = codec.Encode(data, Base58FormatProvider.Ripple);
Console.WriteLine(bitcoin);
Console.WriteLine(flickr);
Console.WriteLine(ripple);using OnixLabs.Core.Text;
// Leading zero bytes are represented as the first character of the alphabet ('1' for Bitcoin).
byte[] data = [0x00, 0x00, 0xDE, 0xAD];
Base58Codec codec = new();
string encoded = codec.Encode(data, Base58FormatProvider.Bitcoin);
// encoded starts with "11" — one character per leading zero byte.ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions