Base58

Base58 encode and decode

Base58 is a group of binary-to-text encoding schemes used to represent large integers as alphanumeric text. It is similar to Base64 but has been modified to avoid both non-alphanumeric characters and letters which might look ambiguous when printed. It is therefore designed for human users who manually enter the data, copying from some visual source, but also allows easy copy and paste because a double-click will usually select the whole string.

Contents

How it works?

The Base58 encoding format is designed for use in Bitcoin and is used in many other cryptocurrencies. It offers a balance between compact performance, readability, definition and error prevention. Base58 is a subset of Base64 that uses lowercase and uppercase letters and numbers, but without some characters that are often mistaken for each other and can be identical in some fonts. In particular, Base58 is Base64 without the number 0 (zero), O (capital letter O), l (small L), I (large i), and the characters “+” and “/”. Or, simply put, it is a set of uppercase and lowercase letters and numbers without the four (0, O, L, I) mentioned above.In contrast to Base64, the digits of the encoding do not line up well with byte boundaries of the original data. For this reason, the method is well-suited to encode large integers, but not designed to encode longer portions of binary data. The actual order of letters in the alphabet depends on the application, which is the reason why the term “Base58” alone is not enough to fully describe the format. A variant, Base56, excludes 1 (one) and o (lowercase o) compared to Base 58.

Base58Check is a Base58 encoding format that unambiguously encodes the type of data in the first few characters and includes an error detection code in the last few characters.[1]

Base58 Encode and Decode

With Base58, private key can be converted to a public key, but a public key cannot be converted to a private key. The bitcoin address is not really a public key, but its hash. So, the bitcoin address corresponding to the given above public key will be: 172YRdGzPqyXm9rm1EWKwPXTRsmcApoPq6.

Bitcoin address is not presented in hexadecimal form as secret and public keys. This is because for him, Bitcoin uses a “compressed” format, known as base58check. Bitcoin Base58 suggests that it uses 58 different letters and numbers (zero, O, l and I are omitted, as they are too easily confused with each other). Base58 is a number with a base 58-similar to the usual for us binary (base 2), decimal (base 10) or hexadecimal (base 16) representation. For example, the decimal number 31337 in binary form would be ‘111101001101001’, in hexadecimal it would be ‘7A69’, and at base 58 it would just be ‘AKJ’. Well, the suffix check in the description of the format means that before the translation of a number in base58 it still adds a four-byte “check sum”. All bitcoin libraries (which we will talk about later) make a transfer to base58check automatically, so you do not have to program it specifically.

Alphabet Base58

Blockchain tutorial: Base-58 encoding

Base58 alphabet: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
To add additional protection against typos or transcription errors, Base58Check is a Base58 with built — in error checking code. Four bytes of checksum are added to the end of the encoded data. The checksum is obtained by hashing the encoded data and can therefore be used to detect and prevent transcription and typo errors. The software will calculate the checksum of the data when decoding and compare it with the checksum of the code. The mismatch will indicate an error and the Base58Check data will be invalid. For example, such a check prevents the possibility of sending funds to a non-existent Bitcoin address and thus losing funds.

To convert data (numeric) to Base58Check format, you must first add a prefix to The data, called a “version byte”, which is used to determine the type of data to be encoded. For example, in the case of a Bitcoin address, the prefix is 0 (0x00 in the hex system), while the prefix for the private key is 128 (0x80 in the hex system).

Next, we compute a checksum of the “double-SHA”, in the sense that the used hash algorithm is SHA256 twice on the previous result (prefix and data).
checksum = SHA256(SHA256(prefix+data))
From the resulting 32-byte hash (hash of hash), we take only the first four bytes. These four bytes serve as the error-checking code, or checksum. This checksum is then added to the end of the line. The result consists of three elements: prefix, data, and checksum. This result is encoded using the Base58 alphabet described earlier.

Base58 converters

Application Alphabet Excluded Base64 characters
Bitcoin addresses[2], IPFS hashes[3]

123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz

IOl0+/
Ripple addresses[4] rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz[5] IOl0+/
Short URLs for Flickr[6] 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ[7] IOl0+/

Links

See also

References

  1. Andreas M. Antonopoulos. “Mastering Bitcoin”. section “Base58 and Base58Check Encoding”. (O’Reilly, 2015).
  2. Technical background of version 1 Bitcoin addresses
  3. IPFS API Implementation Doc
  4. Accounts
  5. https://wiki.ripple.com/Encodings
  6. Photo Source URLs
  7. Flickr API


See Also on BitcoinWiki