Table of Contents

Base58Check Class

Definition

Namespace
Bodu.Text.Encoding
Assembly
Bodu.Text.Encoding.dll
Package
Bodu.Text.Encoding 1.0.0
Source
Base58Check.cs

Provides Base58Check encoding and decoding - a Base58 superset used by Bitcoin addresses, WIF private keys, and related protocols. The encoder appends a four-byte checksum derived from the leading bytes of SHA-256(SHA-256(payload)) so that mistyped or truncated input can be detected at decode time.

public static class Base58Check
Inheritance
Base58Check
Inherited Members

Examples

// Build a Bitcoin P2PKH address: 0x00 version byte + 20-byte hash160.
byte[] payload = new byte[21];
payload[0] = 0x00;                                          // mainnet version
hash160.CopyTo(payload.AsSpan(1));                          // 20-byte RIPEMD-160(SHA-256(pubkey))

string address = Base58Check.Encode(payload);               // "1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2" (example)

// Decoder verifies the trailing checksum and strips it; throws on mismatch.
byte[] decoded = Base58Check.Decode(address);               // 21 bytes - original version + hash160

Remarks

The encoded form is Base58(payload || SHA-256(SHA-256(payload))[0..4]). The decoded form recovers the original payload after verifying the checksum; if the checksum disagrees the decoder fails.

Leading zero bytes in the payload are preserved by the underlying Base58 encoder as leading alphabet[0] characters (typically 1 in the Bitcoin/Flickr alphabet).

Methods

Decode(ReadOnlySpan<char>, Base58Variant, BaseFormatStyles)

Decodes a Base58Check encoded string back to the original payload, throwing if the trailing four-byte checksum does not match.

public static byte[] Decode(ReadOnlySpan<char> source, Base58Variant variant = Base58Variant.BitcoinFlickr, BaseFormatStyles styles = BaseFormatStyles.None)

Parameters

source ReadOnlySpan<char>

The Base58Check encoded input.

variant Base58Variant

The Base58 variant.

styles BaseFormatStyles

Parsing styles.

Returns

byte[]

The decoded payload bytes (the trailing checksum is verified and stripped).

Exceptions

ArgumentOutOfRangeException

Thrown when variant is undefined.

FormatException

Thrown when source contains characters outside the variant alphabet, decodes to fewer than four bytes, or whose trailing four-byte checksum disagrees with the computed value.

Encode(ReadOnlySpan<byte>, Base58Variant)

Encodes payload using Base58Check - appends a four-byte SHA-256 squared checksum and Base58 encodes the concatenation.

public static string Encode(ReadOnlySpan<byte> payload, Base58Variant variant = Base58Variant.BitcoinFlickr)

Parameters

payload ReadOnlySpan<byte>

The bytes to encode.

variant Base58Variant

The Base58 variant.

Returns

string

A Base58Check encoded string.

Exceptions

ArgumentOutOfRangeException

Thrown when variant is undefined.

GetMaxDecodedLength(int)

Returns an upper bound on the payload byte count that decoding charCount characters can produce, after stripping the four-byte checksum suffix.

public static int GetMaxDecodedLength(int charCount)

Parameters

charCount int

The input character count.

Returns

int

An upper bound on the payload byte count, or 0 when charCount is too small to contain a checksum.

Exceptions

ArgumentOutOfRangeException

Thrown when charCount is negative.

GetMaxEncodedLength(int)

Returns an upper bound on the number of characters required to encode a payloadByteCount -byte payload via Base58Check (payload + four checksum bytes).

public static int GetMaxEncodedLength(int payloadByteCount)

Parameters

payloadByteCount int

The payload byte count.

Returns

int

An upper bound on the encoded character count.

Exceptions

ArgumentOutOfRangeException

Thrown when payloadByteCount is negative.

IsValid(ReadOnlySpan<char>, Base58Variant, BaseFormatStyles)

Indicates whether source is a valid Base58Check input under the supplied variant - that is, every character is in the variant alphabet and the trailing four-byte checksum matches the decoded payload.

public static bool IsValid(ReadOnlySpan<char> source, Base58Variant variant = Base58Variant.BitcoinFlickr, BaseFormatStyles styles = BaseFormatStyles.None)

Parameters

source ReadOnlySpan<char>

The Base58Check encoded input.

variant Base58Variant

The Base58 variant.

styles BaseFormatStyles

Parsing styles.

Returns

bool

true when the input is a structurally and cryptographically valid Base58Check encoding; otherwise false.

Exceptions

ArgumentOutOfRangeException

Thrown when variant is undefined.

TryDecode(ReadOnlySpan<char>, Span<byte>, out int, Base58Variant, BaseFormatStyles)

Attempts to decode a Base58Check encoded string into destination, returning false when the checksum disagrees, the destination is too small, or the input is malformed.

public static bool TryDecode(ReadOnlySpan<char> source, Span<byte> destination, out int bytesWritten, Base58Variant variant = Base58Variant.BitcoinFlickr, BaseFormatStyles styles = BaseFormatStyles.None)

Parameters

source ReadOnlySpan<char>

The Base58Check encoded input.

destination Span<byte>

The destination span receiving the payload bytes.

bytesWritten int

When this method returns, contains the number of bytes written.

variant Base58Variant

The Base58 variant.

styles BaseFormatStyles

Parsing styles.

Returns

bool

true when decoding succeeds and the checksum verifies; otherwise false.

Exceptions

ArgumentOutOfRangeException

Thrown when variant is undefined.

Applies to

ProductVersions
.NET8, 10