Repository navigation
OnixLabs.Core.Text.Base58
Represents a Base-58 encoded value: an immutable wrapper around a sequence of bytes whose textual representation is produced and consumed using a Base-58 alphabet.
| Namespace | OnixLabs.Core.Text |
| Assembly | OnixLabs.Core.dll (NuGet) |
| Inheritance |
object → ValueType → Base58
|
| Implements |
IBaseValue<Base58> (and through it IValueEquatable<Base58>, ISpanParsable<Base58>, IBinaryConvertible, ISpanFormattable) |
public readonly partial struct Base58 : IBaseValue<Base58>Base58 models a binary value together with its Base-58 textual form. Base-58 is a binary-to-text encoding that uses 58 alphanumeric characters, omitting the visually ambiguous characters 0 (zero), O (capital o), I (capital i), and l (lower-case L), along with the + and / characters used by Base-64. It is widely used by cryptocurrency and distributed-system projects where human transcription of values is common.
The struct stores its underlying value as a defensive copy of the bytes supplied to its public constructors, which preserves value-type immutability. The encoding and decoding work is delegated to the Base-58 codec exposed through OnixLabs.Core.Text.Codecs; the specific alphabet used during encoding and decoding is selected by an IFormatProvider, namely a Base58FormatProvider (for example, Bitcoin, Flickr, or Ripple).
Note The alphabet variants supported by this type are alphabet substitutions only. They do not add or verify a checksum; this type does not implement the Base58Check variant used by some Bitcoin address formats.
Equality is based on value semantics: two Base58 instances are equal when their underlying byte sequences are equal. The default (parameterless) ToString() encodes the value using the Bitcoin alphabet.
using OnixLabs.Core.Text;
byte[] data = [1, 2, 3, 4];
Base58 value = new(data);
// Implicit conversion from a byte span is also supported.
Base58 converted = data.AsSpan();using System.Text;
using OnixLabs.Core.Text;
// The characters are first encoded to bytes using the specified (or default) encoding,
// then wrapped as a Base58 value.
Base58 value = new("Hello, World!", Encoding.UTF8);using OnixLabs.Core.Text;
Base58 value = new(new byte[] { 0, 1, 2, 3 });
// Encode using the default (Bitcoin) alphabet.
string bitcoin = value.ToString();
// Encode using an explicit alphabet.
string flickr = value.ToString(Base58FormatProvider.Flickr);
// Round-trip back to a Base58 value.
Base58 parsed = Base58.Parse(bitcoin);
if (Base58.TryParse(bitcoin, null, out Base58 result))
{
// result is equal to value.
}Base58 value = new(new byte[] { 10, 20, 30 });
ReadOnlySpan<byte> span = value.AsReadOnlySpan();
ReadOnlyMemory<byte> memory = value.AsReadOnlyMemory();| Constructor | Description |
|---|---|
Base58(ReadOnlySpan<byte>) |
Initializes a new instance from a read-only span of bytes. |
Base58(ReadOnlyMemory<byte>) |
Initializes a new instance from a read-only memory region of bytes. |
Base58(ReadOnlySequence<byte>) |
Initializes a new instance from a read-only sequence of bytes. |
Base58(ReadOnlySpan<char>, Encoding?) |
Initializes a new instance from a read-only span of characters using the specified encoding. |
Base58(ReadOnlyMemory<char>, Encoding?) |
Initializes a new instance from a read-only memory region of characters using the specified encoding. |
Base58(ReadOnlySequence<char>, Encoding?) |
Initializes a new instance from a read-only sequence of characters using the specified encoding. |
public Base58(ReadOnlySpan<byte> value)Initializes a new instance of the Base58 struct. A defensive copy of the supplied bytes is taken.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<byte> |
The value from which to initialize the new Base58 instance. |
public Base58(ReadOnlyMemory<byte> value)Initializes a new instance of the Base58 struct. A defensive copy of the supplied bytes is taken.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlyMemory<byte> |
The value from which to initialize the new Base58 instance. |
public Base58(ReadOnlySequence<byte> value)Initializes a new instance of the Base58 struct. A defensive copy of the supplied bytes is taken.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySequence<byte> |
The value from which to initialize the new Base58 instance. |
public Base58(ReadOnlySpan<char> value, Encoding? encoding = null)Initializes a new instance of the Base58 struct from text. The characters are transformed into bytes using the specified encoding before being stored.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The value from which to initialize the new Base58 instance. |
encoding |
Encoding? |
The Encoding used to transform the specified value, or null to use the default encoding. |
public Base58(ReadOnlyMemory<char> value, Encoding? encoding = null)Initializes a new instance of the Base58 struct from text. The characters are transformed into bytes using the specified encoding before being stored.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlyMemory<char> |
The value from which to initialize the new Base58 instance. |
encoding |
Encoding? |
The Encoding used to transform the specified value, or null to use the default encoding. |
public Base58(ReadOnlySequence<char> value, Encoding? encoding = null)Initializes a new instance of the Base58 struct from text. The characters are transformed into bytes using the specified encoding before being stored.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySequence<char> |
The value from which to initialize the new Base58 instance. |
encoding |
Encoding? |
The Encoding used to transform the specified value, or null to use the default encoding. |
| Method | Returns | Description |
|---|---|---|
Parse(string, IFormatProvider?) |
Base58 |
Decodes the specified Base-58 string into a Base58 value. |
Parse(ReadOnlySpan<char>, IFormatProvider?) |
Base58 |
Decodes the specified Base-58 character span into a Base58 value. |
TryParse(string?, IFormatProvider?, out Base58) |
bool |
Attempts to decode the specified Base-58 string into a Base58 value. |
TryParse(ReadOnlySpan<char>, IFormatProvider?, out Base58) |
bool |
Attempts to decode the specified Base-58 character span into a Base58 value. |
| Method | Returns | Description |
|---|---|---|
AsReadOnlySpan() |
ReadOnlySpan<byte> |
Gets a read-only span over the underlying bytes. |
AsReadOnlyMemory() |
ReadOnlyMemory<byte> |
Gets a read-only memory region over the underlying bytes. |
Equals(Base58) |
bool |
Determines whether this value equals another Base58 value. |
Equals(object?) |
bool |
Determines whether this value equals another object. |
GetHashCode() |
int |
Returns a content-based hash code. |
ToString() |
string |
Encodes the value using the Bitcoin alphabet. |
ToString(IFormatProvider?) |
string |
Encodes the value using the specified format provider. |
ToString(string?, IFormatProvider?) |
string |
Encodes the value using the specified format and format provider. |
ToString(ReadOnlySpan<char>, IFormatProvider?) |
string |
Encodes the value using the specified format and format provider. |
public static Base58 Parse(string value, IFormatProvider? provider = null)Decodes the specified Base-58 string into a Base58 value.
| Parameter | Type | Description |
|---|---|---|
value |
string |
The Base-58 encoded string to decode. |
provider |
IFormatProvider? |
The format provider that supplies the Base-58 alphabet, or null to use the default. |
Returns — A new Base58 value decoded from value.
public static Base58 Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null)Decodes the specified Base-58 character span into a Base58 value.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The Base-58 encoded characters to decode. |
provider |
IFormatProvider? |
The format provider that supplies the Base-58 alphabet, or null to use the default. |
Returns — A new Base58 value decoded from value.
public static bool TryParse(string? value, IFormatProvider? provider, out Base58 result)Attempts to decode the specified Base-58 string into a Base58 value.
| Parameter | Type | Description |
|---|---|---|
value |
string? |
The Base-58 encoded string to decode. |
provider |
IFormatProvider? |
The format provider that supplies the Base-58 alphabet, or null to use the default. |
result |
out Base58 |
When this method returns, contains the decoded value if decoding succeeded; otherwise, the default value. |
Returns — true if value was decoded successfully; otherwise, false.
public static bool TryParse(ReadOnlySpan<char> value, IFormatProvider? provider, out Base58 result)Attempts to decode the specified Base-58 character span into a Base58 value.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The Base-58 encoded characters to decode. |
provider |
IFormatProvider? |
The format provider that supplies the Base-58 alphabet, or null to use the default. |
result |
out Base58 |
When this method returns, contains the decoded value if decoding succeeded; otherwise, the default value. |
Returns — true if value was decoded successfully; otherwise, false.
public ReadOnlySpan<byte> AsReadOnlySpan()Gets a read-only span over the underlying bytes of the current Base58 value.
Returns — A ReadOnlySpan<byte> over the underlying value.
public ReadOnlyMemory<byte> AsReadOnlyMemory()Gets a read-only memory region over the underlying bytes of the current Base58 value.
Returns — A ReadOnlyMemory<byte> over the underlying value.
public bool Equals(Base58 other)Determines whether the current Base58 value is equal to another Base58 value by comparing their underlying byte sequences.
Returns — true if the values are equal; otherwise, false.
public override bool Equals(object? obj)Determines whether the current Base58 value is equal to another object.
Returns — true if obj is a Base58 with an equal underlying value; otherwise, false.
public override int GetHashCode()Returns a hash code derived from the content of the underlying byte sequence. Equal values produce equal hash codes; like HashCode, the result is randomised per process and must not be persisted.
Returns — A 32-bit signed integer hash code.
public override string ToString()Encodes the current value into its Base-58 string representation using the Bitcoin alphabet (Base58FormatProvider.Bitcoin).
Returns — The Base-58 encoded string representation of the value.
public string ToString(IFormatProvider? formatProvider)Encodes the current value into its Base-58 string representation using the alphabet supplied by the specified format provider.
| Parameter | Type | Description |
|---|---|---|
formatProvider |
IFormatProvider? |
The format provider that supplies the Base-58 alphabet, or null to use the default. |
Returns — The Base-58 encoded string representation of the value.
public string ToString(string? format, IFormatProvider? formatProvider)Encodes the current value into its Base-58 string representation using the specified format and format provider.
| Parameter | Type | Description |
|---|---|---|
format |
string? |
The format to use. |
formatProvider |
IFormatProvider? |
The format provider that supplies the Base-58 alphabet, or null to use the default. |
Returns — The Base-58 encoded string representation of the value.
public string ToString(ReadOnlySpan<char> format, IFormatProvider? formatProvider)Encodes the current value into its Base-58 string representation using the specified format and format provider.
| Parameter | Type | Description |
|---|---|---|
format |
ReadOnlySpan<char> |
The format to use. |
formatProvider |
IFormatProvider? |
The format provider that supplies the Base-58 alphabet, or null to use the default. |
Returns — The Base-58 encoded string representation of the value.
| Operator | Description |
|---|---|
operator == |
Determines whether two Base58 values are equal. |
operator != |
Determines whether two Base58 values are not equal. |
implicit operator Base58(ReadOnlySpan<byte>) |
Converts a read-only span of bytes to a Base58 value. |
implicit operator Base58(ReadOnlyMemory<byte>) |
Converts a read-only memory region of bytes to a Base58 value. |
implicit operator Base58(ReadOnlySequence<byte>) |
Converts a read-only sequence of bytes to a Base58 value. |
implicit operator Base58(ReadOnlySpan<char>) |
Converts a read-only span of characters to a Base58 value using the default encoding. |
implicit operator Base58(ReadOnlyMemory<char>) |
Converts a read-only memory region of characters to a Base58 value using the default encoding. |
implicit operator Base58(ReadOnlySequence<char>) |
Converts a read-only sequence of characters to a Base58 value using the default encoding. |
public static bool operator ==(Base58 left, Base58 right)Performs an equality comparison between two Base58 values. Returns true if left is equal to right; otherwise, false.
public static bool operator !=(Base58 left, Base58 right)Performs an inequality comparison between two Base58 values. Returns true if left is not equal to right; otherwise, false.
public static implicit operator Base58(ReadOnlySpan<byte> value)
public static implicit operator Base58(ReadOnlyMemory<byte> value)
public static implicit operator Base58(ReadOnlySequence<byte> value)
public static implicit operator Base58(ReadOnlySpan<char> value)
public static implicit operator Base58(ReadOnlyMemory<char> value)
public static implicit operator Base58(ReadOnlySequence<char> value)Implicitly converts the specified value into a new Base58 instance. The byte-based conversions wrap a defensive copy of the bytes; the character-based conversions first transform the characters into bytes using the default encoding.
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