Class Base58
- All Implemented Interfaces:
BinaryDecoder,BinaryEncoder,Decoder,Encoder
Base58 is a binary-to-text encoding scheme that uses a 58-character alphabet to encode data. It avoids characters that can be confused (0/O, I/l, +/) and is commonly used in Bitcoin and other blockchain systems.
This implementation accumulates data internally until EOF is signaled, at which point the entire input is converted using BigInteger arithmetic. This is necessary because Base58 encoding/decoding requires access to the complete data to properly handle leading zeros.
This class is thread-safe for read operations but the Context object used during encoding/decoding should not be shared between threads.
The Base58 alphabet is:
123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
This excludes: 0, I, O, and l.
- Since:
- 1.22.0
- See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class org.apache.commons.codec.binary.BaseNCodec
BaseNCodec.AbstractBuilder<T,B extends BaseNCodec.AbstractBuilder<T, B>>, BaseNCodec.Context -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final BigIntegerprivate static final byte[]This array is a lookup table that translates Unicode characters drawn from the "Base58 Alphabet" into their numeric equivalents (0-57).private static final byte[]private static final byte[]Base58 alphabet: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz (excludes: 0, I, O, l).Fields inherited from class org.apache.commons.codec.binary.BaseNCodec
CHUNK_SEPARATOR, decodeTable, DECODING_POLICY_DEFAULT, EMPTY_BYTE_ARRAY, encodeTable, EOF, lineLength, MASK_8BITS, MIME_CHUNK_SIZE, pad, PAD, PAD_DEFAULT, PEM_CHUNK_SIZE -
Constructor Summary
ConstructorsConstructorDescriptionBase58()Constructs a Base58 codec used for encoding and decoding.Base58(Base58.Builder builder) Constructs a Base58 codec used for encoding and decoding with custom configuration. -
Method Summary
Modifier and TypeMethodDescriptionstatic Base58.Builderbuilder()Creates a new Builder.private voidconvertFromBase58(byte[] base58, BaseNCodec.Context context) Converts Base58 encoded data to binary.private byte[]convertToBase58(byte[] accumulate, BaseNCodec.Context context) Converts accumulated binary data to Base58 encoding.(package private) voiddecode(byte[] array, int offset, int length, BaseNCodec.Context context) Decodes the given Base58 encoded data.(package private) voidencode(byte[] array, int offset, int length, BaseNCodec.Context context) Encodes the given binary data as Base58.private StringBuildergetStringBuilder(byte[] accumulate) Builds the Base58 string representation of the given binary data.protected booleanisInAlphabet(byte value) Returns whether or not theoctetis in the Base58 alphabet.Methods inherited from class org.apache.commons.codec.binary.BaseNCodec
available, containsAlphabetOrPad, decode, decode, decode, encode, encode, encode, encodeAsString, encodeToString, ensureBufferSize, getChunkSeparator, getCodecPolicy, getDefaultBufferSize, getEncodedLength, getLength, hasData, isInAlphabet, isInAlphabet, isInAlphabet, isStrictDecoding, isWhiteSpace, readResults
-
Field Details
-
BASE
-
EMPTY
private static final byte[] EMPTY -
ENCODE_TABLE
private static final byte[] ENCODE_TABLEBase58 alphabet: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz (excludes: 0, I, O, l). -
DECODE_TABLE
private static final byte[] DECODE_TABLEThis array is a lookup table that translates Unicode characters drawn from the "Base58 Alphabet" into their numeric equivalents (0-57). Characters that are not in the Base58 alphabet are marked with -1.
-
-
Constructor Details
-
Base58
public Base58()Constructs a Base58 codec used for encoding and decoding. -
Base58
Constructs a Base58 codec used for encoding and decoding with custom configuration.- Parameters:
builder- the builder with custom configuration.
-
-
Method Details
-
builder
Creates a new Builder.To configure a new instance, use a
Base58.Builder. For example:Base58 base58 = Base58.builder() .setEncode(true) .get()
- Returns:
- a new Builder.
-
convertFromBase58
Converts Base58 encoded data to binary.Uses BigInteger arithmetic to convert the Base58 string to binary data. Leading '1' characters in the Base58 encoding represent leading zero bytes in the binary data.
- Parameters:
base58- the Base58 encoded data.context- the context for this decoding operation.- Throws:
IllegalArgumentException- if the Base58 data contains invalid characters.
-
convertToBase58
Converts accumulated binary data to Base58 encoding.Uses BigInteger arithmetic to convert the binary data to Base58. Leading zeros in the binary data are represented as '1' characters in the Base58 encoding.
- Parameters:
accumulate- the binary data to encode.context- the context for this encoding operation.- Returns:
- the buffer containing the encoded data.
-
decode
Decodes the given Base58 encoded data.This implementation accumulates data internally. When length is less than 0 (EOF), the accumulated data is converted from Base58 to binary.
- Specified by:
decodein classBaseNCodec- Parameters:
array- the byte array containing Base58 encoded data.offset- the offset in the array to start from.length- the number of bytes to decode, or negative to signal EOF.context- the context for this decoding operation.
-
encode
Encodes the given binary data as Base58.This implementation accumulates data internally. When length is less than 0 (EOF), the accumulated data is converted to Base58.
- Specified by:
encodein classBaseNCodec- Parameters:
array- the byte array containing binary data to encode.offset- the offset in the array to start from.length- the number of bytes to encode, or negative to signal EOF.context- the context for this encoding operation.
-
getStringBuilder
Builds the Base58 string representation of the given binary data.Converts binary data to a BigInteger and divides by 58 repeatedly to get the Base58 digits. Handles leading zeros by counting them and appending '1' for each leading zero byte.
- Parameters:
accumulate- the binary data to convert.- Returns:
- a StringBuilder with the Base58 representation (not yet reversed).
-
isInAlphabet
protected boolean isInAlphabet(byte value) Returns whether or not theoctetis in the Base58 alphabet.- Specified by:
isInAlphabetin classBaseNCodec- Parameters:
value- The value to test.- Returns:
trueif the value is defined in the Base58 alphabetfalseotherwise.
-