HTTP: The Definitive Guide
by David Gourley, Brian Totty, Marjorie Sayer, Anshu Aggarwal, Sailu Reddy
Base-64 Padding
Base-64 encoding takes a sequence of 8-bit bytes and segments the bit stream into 6-bit chunks. It is unlikely that the sequence of bits will divide evenly into 6-bit pieces. When the bit sequence does not divide evenly into 6-bit pieces, the bit sequence is padded with zero bits at the end to make the length of the bit sequence a multiple of 24 (the least common multiple of 6 and 8 bits).
When encoding the padded bit string, any group of 6 bits that is completely padding (containing no bits from the original data) is represented by a special 65th symbol: " =”. If a group of 6 bits is partially padded, the padding bits are set to zero.
Table E-2 shows examples of padding. The initial input string “a:a” is 3 bytes long, or 24 bits. 24 is a multiple of 6 and 8, so no padding is required. The resulting base 64-encoded string is “YTph”.
Table E-2. Base-64 padding examples
|
Input data |
Binary sequence (padding noted as “x”) |
Encoded data |
|---|---|---|
a:a |
011000 010011 101001 100001 |
YTph |
a:aa |
011000 010011 101001 100001 011000 01xxxx xxxxxx xxxxxx |
YTphYQ== |
a:aaa |
011000 010011 101001 100001 011000 010110 0001xx xxxxxx |
YTphYWE= |
a:aaaa |
011000 010011 101001 100001 011000 010110 000101 100001 |
YTphYWFh |
However, when another character is added, the input string grows to 32 bits long. The next smallest multiple of 6 and 8 is 48 bits, so 16 bits of padding are added. The first 4 bits of padding are mixed with data bits. The resulting 6-bit group, 01xxxx, is treated as 010000, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access