What five bits of a byte become in RFC 4648 Base32
RFC 4648 Base32 takes the UTF-8 bytes of a text and writes them five bits at a time. Thirty-two symbols cover those five bits: A through Z, then 2 through 7. The digits 0, 1, 8, and 9 are not in the alphabet, so a handwritten O is not a zero and a handwritten I is not a one. Encoding writes uppercase. On the way back, A and a are the same symbol.
Eight symbols carry forty bits, which is five bytes. A shorter input ends on a partial group. The encoder still emits one last symbol from the remaining bits and does not append =. The RFC allows = so the length becomes a multiple of eight. MZXQ and MZXQ==== are both the two bytes of fo. Spaces are not data. A trailing = is padding, not another bit.
The same bytes have other spellings. Base64 groups six bits and uses a larger alphabet, including lowercase. Hex encode and decode writes four bits as one digit, two digits per byte. Base32 is the longer, case-insensitive spelling of those bytes. It is not Crockford Base32 and not base32hex. Those alphabets assign different values, and a 0 or a 1 in them is not a symbol here.
How the alphabet, padding, and the other Base32 names line up
Why the alphabet is A through Z and 2 through 7, with no 0 or 1
- Five bits have thirty-two patterns. Twenty-six letters and the six digits 2 through 7 fill that set. 0, 1, 8, and 9 are outside it.
- The missing 0 and 1 are the point of the alphabet. O and 0, and I and 1, are easy to mix when a person retypes a seed. 8 and 9 are simply unused.
- Encoding emits only uppercase. Decoding folds lowercase to uppercase and then accepts A through Z and 2 through 7. Any other character, including 0 or 8, is not a symbol.
Why the = padding can be missing and the letter case does not matter
- A full group is eight characters for five bytes. fo is two bytes, so the data symbols are MZXQ. The RFC pads that to MZXQ====. Both spellings are the same bytes.
- hello is five bytes, exactly one full group, so it is NBSWY3DP with no room for padding. A missing = on a shorter string is still the same data.
- Spaces and line breaks are separators. Trailing = signs are padding. An = in the middle is not padding, and a symbol outside the alphabet is not a shortened form of a valid string.
How Base32 of the same UTF-8 bytes differs from Base64 and from hex
- All three start from the UTF-8 bytes. Base32 takes five bits, Base64 takes six, and hex takes four. The groups do not land on the same character boundaries.
- hello is NBSWY3DP in this alphabet, aGVsbG8= in Base64, and 68656c6c6f in hex. None of those strings is a different message. They are three writings of 68 65 6C 6C 6F.
- Base32 is longer than Base64 and shorter than a binary listing of the same bits. It stays inside one case, which Base64 does not. The bytes themselves are unchanged.
Why an OTP seed in this alphabet usually does not come back as a sentence
- An otpauth secret is the raw HMAC key, written with this alphabet so a person can type it. The key is random bytes. It was not a sentence that someone encoded.
- Valid symbols only mean the bits can be rebuilt. Those bits are text when they are valid UTF-8. A key that is not UTF-8 is rejected as text even though every character was in the alphabet.
- A string that is valid UTF-8 is still not the account name or the password a person remembers. NBSWY3DP is hello only because those five letters were the input. A twenty-byte key is not five letters.
Where a Base32 string is bytes rather than the words around it
An authenticator setup string that a person has to retype
The secret field of an otpauth URI is RFC 4648 Base32 of the HMAC key, often without = padding. Uppercase and lowercase are the same symbols, which is why the alphabet can be read aloud. The surrounding account name is ordinary text and is not inside that Base32. Decoding the secret as a sentence asks the key bytes to be UTF-8 words, which they usually are not.
A short value whose padded and unpadded forms look different
fo encodes to MZXQ. The padded RFC form is MZXQ====. The four equals signs fill the group out to eight characters and add no bits. hello needs no padding because five bytes already fill eight symbols, NBSWY3DP. Comparing the two forms by string equality fails when one side kept the padding and the other dropped it.
The same short word written as Base32, Base64, and hex
hello is the five bytes 68 65 6C 6C 6F. Base32 spells them NBSWY3DP. Base64 spells them aGVsbG8=, which is the subject of Base64. Hex spells them 68656c6c6f, two digits per byte, which is the subject of hex encode and decode. A decoder for one alphabet will not accept the other spelling.