Base58Check Class
Definition
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
sourceReadOnlySpan<char>The Base58Check encoded input.
variantBase58VariantThe Base58 variant.
stylesBaseFormatStylesParsing styles.
Returns
- byte[]
The decoded payload bytes (the trailing checksum is verified and stripped).
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.- FormatException
Thrown when
sourcecontains 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
payloadReadOnlySpan<byte>The bytes to encode.
variantBase58VariantThe Base58 variant.
Returns
- string
A Base58Check encoded string.
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis 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
charCountintThe input character count.
Returns
- int
An upper bound on the payload byte count, or
0whencharCountis too small to contain a checksum.
Exceptions
- ArgumentOutOfRangeException
Thrown when
charCountis 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
payloadByteCountintThe payload byte count.
Returns
- int
An upper bound on the encoded character count.
Exceptions
- ArgumentOutOfRangeException
Thrown when
payloadByteCountis 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
sourceReadOnlySpan<char>The Base58Check encoded input.
variantBase58VariantThe Base58 variant.
stylesBaseFormatStylesParsing styles.
Returns
- bool
true when the input is a structurally and cryptographically valid Base58Check encoding; otherwise false.
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis 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
sourceReadOnlySpan<char>The Base58Check encoded input.
destinationSpan<byte>The destination span receiving the payload bytes.
bytesWrittenintWhen this method returns, contains the number of bytes written.
variantBase58VariantThe Base58 variant.
stylesBaseFormatStylesParsing styles.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |