Base58: Encoding built for Humans
Base58 is an encoding standard that makes it easy to represent binary data as readable strings. At its core, it’s designed to convert bits (binary zeros and ones) into characters that are safe for both humans and machine parsing.
For example this byte array in binary 00000100 10101110 11111111
in decimal represents 306175
and by repeatedly dividing by 58 and recording the remainders and using the Base58 alphabeth we get the base58 encoded version 2a1Y
.
306175 ÷ 58 = 5278, remainder 31 → Base58 character 'Y'5278 ÷ 58 = 91, remainder 0 → Base58 character '1'91 ÷ 58 = 1, remainder 33 → Base58 character 'a'1 ÷ 58 = 0, remainder 1 → Base58 character '2'
Base58 alphabeth:
Decimal | Base58 Character | Decimal | Base58 Character | Decimal | Base58 Character |
---|---|---|---|---|---|
0 | 1 | 20 | M | 40 | h |
1 | 2 | 21 | N | 41 | i |
2 | 3 | 22 | P | 42 | j |
3 | 4 | 23 | Q | 43 | k |
4 | 5 | 24 | R | 44 | m |
5 | 6 | 25 | S | 45 | n |
6 | 7 | 26 | T | 46 | o |
7 | 8 | 27 | U | 47 | p |
8 | 9 | 28 | V | 48 | q |
9 | A | 29 | W | 49 | r |
10 | B | 30 | X | 50 | s |
11 | C | 31 | Y | 51 | t |
12 | D | 32 | Z | 52 | u |
13 | E | 33 | a | 53 | v |
14 | F | 34 | b | 54 | w |
15 | G | 35 | c | 55 | x |
16 | H | 36 | d | 56 | y |
17 | J | 37 | e | 57 | z |
18 | K | 38 | f | ||
19 | L | 39 | g |
There’s an interesting detail in its design that sets it apart from other encoding schemes. It excludes the digit “0”, uppercase “O”, uppercase “I”, and lowercase “l.” Why? These characters are visually similar and can easily be confused. By leaving them out, Base58 ensures that people won’t mistake “0” for “O” or “I” for “l” when reading or comparing the encoded values.
Another advantage of Base58 is its single, stable alphabet that is compatible with URLs and Filenames. Base64 for example has multiple alphabets as detailed in RFC 4648 to target these use-cases and this alone can cause bugs and confusion.
However, this design choice does come with a trade-off. Base58 requires around 25% more characters to represent the same amount of data compared to Base64.
Update: As Paul Miller highlighted in his tweet, Base58 has a complexity upper bound of O(n^2), making it impractical for processing data larger than 1KB due to potential crashes.
Published at: 2024-11-07
Updated at: 2024-11-07