← All Exercises

Base32 Encoder and Decoder

intermediatejavascript~25min

Background

Base32 trades compactness for case-insensitive, typo-resistant text: thirty-two symbols survive human transcription and case folding, which is why TOTP secrets and DNS labels use it.

Problem

Implement base32 per RFC 4648 with the alphabet ABCDEFGHIJKLMNOPQRSTUVWXYZ234567 and = padding. Encoding packs bytes into five-bit groups from the most significant end, pads the final group with zero bits, and appends = characters so the output length is a multiple of eight. Decode by reversing the process and dropping padding. Output the encoded string and the decoded round trip.

Input

A JSON object {"text": string} of arbitrary printable text.

Output

A JSON object {"encoded": string, "decoded": string} where decoded equals the input text.

Sample tests

Sample 1

Input: {"text":"Hello!"}
Expected: {"encoded":"JBSWY3DPEE======","decoded":"Hello!"}

Sample 2

Input: {"text":"abc"}
Expected: {"encoded":"MFRGG===","decoded":"abc"}

Sample 3

Input: {"text":"12345678"}
Expected: {"encoded":"GEZDGNBVGY3TQ===","decoded":"12345678"}

Your solution

Learning objective

Practice bitstream packing, RFC padding rules, and symmetric codec design.