Class Base58

java.lang.Object
org.bitcoinj.core.Base58

public class Base58 extends Object
Base58 is a way to encode Bitcoin addresses (or arbitrary data) as alphanumeric strings.

Note that this is not the same base58 as used by Flickr, which you may find referenced around the Internet.

You may want to consider working with PrefixedChecksummedBytes instead, which adds support for testing the prefix and suffix bytes commonly found in addresses.

Satoshi explains: why base-58 instead of standard base-64 encoding?

  • Don't want 0OIl characters that look the same in some fonts and could be used to create visually identical looking account numbers.
  • A string with non-alphanumeric characters is not as easily accepted as an account number.
  • E-mail usually won't line-break if there's no punctuation to break at.
  • Doubleclicking selects the whole number as one word if it's all alphanumeric.

However, note that the encoding/decoding runs in O(n²) time, so it is not useful for large data.

The basic idea of the encoding is to treat the data bytes as a large number represented using base-256 digits, convert the number to be represented using base-58 digits, preserve the exact number of leading zeros (which are otherwise lost during the mathematical operations on the numbers), and finally represent the resulting base-58 digits as alphanumeric ASCII characters.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final char[]
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    decode(String input)
    Decodes the given base58 string into the original data bytes.
    static byte[]
    Decodes the given base58 string into the original data bytes, using the checksum in the last 4 bytes of the decoded data to verify that the rest are correct.
    static BigInteger
     
    static String
    encode(byte[] input)
    Encodes the given bytes as a base58 string (no checksum is appended).
    static String
    encodeChecked(int version, byte[] payload)
    Encodes the given version and bytes as a base58 string.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ALPHABET

      public static final char[] ALPHABET
  • Constructor Details

    • Base58

      public Base58()
  • Method Details

    • encode

      public static String encode(byte[] input)
      Encodes the given bytes as a base58 string (no checksum is appended).
      Parameters:
      input - the bytes to encode
      Returns:
      the base58-encoded string
    • encodeChecked

      public static String encodeChecked(int version, byte[] payload)
      Encodes the given version and bytes as a base58 string. A checksum is appended.
      Parameters:
      version - the version to encode
      payload - the bytes to encode, e.g. pubkey hash
      Returns:
      the base58-encoded string
    • decode

      public static byte[] decode(String input) throws AddressFormatException
      Decodes the given base58 string into the original data bytes.
      Parameters:
      input - the base58-encoded string to decode
      Returns:
      the decoded data bytes
      Throws:
      AddressFormatException - if the given string is not a valid base58 string
    • decodeToBigInteger

      public static BigInteger decodeToBigInteger(String input) throws AddressFormatException
      Throws:
      AddressFormatException
    • decodeChecked

      public static byte[] decodeChecked(String input) throws AddressFormatException
      Decodes the given base58 string into the original data bytes, using the checksum in the last 4 bytes of the decoded data to verify that the rest are correct. The checksum is removed from the returned data.
      Parameters:
      input - the base58-encoded string to decode (which should include the checksum)
      Throws:
      AddressFormatException - if the input is not base 58 or the checksum does not validate.