Base64 Stream Encoder / Decoder from Scratch
Base64 serializes arbitrary binary streams across ASCII-only protocols (like HTTP headers and emails). Three 8-bit bytes (24 bits) are grouped into an integer and carved into four 6-bit indices (`>> 18`, `>> 12`, `>> 6`, `& 0x3F`) pointing into a 64-character alphabet, with `=` padding remaining slots 🔤📦⚡.
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
def encode_triplet(b0, b1=0, b2=0):
n = (b0 << 16) | (b1 << 8) | b2
return "".join(ALPHABET[(n >> shift) & 0x3F] for shift in (18, 12, 6, 0))
print("Encodes 'Man' to:", encode_triplet(ord("M"), ord("a"), ord("n")))
# 14. Base64 Stream Encoder / Decoder from Scratch Base64 is the bedrock of binary communication across legacy text channels: storing binary cryptographic signatures in JSON Web Tokens (JWT), transmitting attachments in SMTP emails, and embedding inline images into CSS data URIs 🛑. ```python # The 24-Bit Bitwise Math: # Input: 3 bytes = 24 bits # Byte 0: 01001101 ('M' = 77) # Byte 1: 01100001 ('a' = 97) # Byte 2: 01101110 ('n' = 110) # # Combined 24-bit integer: (b0 << 16) | (b1 << 8) | b2 # 01001101 01100001 01101110 # # Carved into 4x 6-bit slices: # Slice 0: (n >> 18) & 0x3F -> 010011 (19 -> 'T') # Slice 1: (n >> 12) & 0x3F -> 010110 (22 -> 'W') # Slice 2: (n >> 6) & 0x3F -> 011000 (24 -> 'F') # Slice 3: (n >> 0) & 0x3F -> 101110 (46 -> 'u') # Output: "TWFu" ``` ### Padding Invariants If the input data length is not divisible by 3: - If 1 byte remains: 2 characters are output, padded with `==`. - If 2 bytes remain: 3 characters are output, padded with `=`. - The final output string length is always a strict multiple of 4 🔤📦⚡. --- ### Interactive Pop-Quiz 🧠#### Why does Base64 encoding expand the size of raw binary data by approximately 33%? - [ ] Every 3 bytes of raw binary data (24 bits) are expanded into 4 ASCII characters (32 bits), yielding a $4/3 \approx 1.33\times$ size increase 📦 - [ ] Because it includes an uncompressed gzip header - [ ] Because ASCII characters consume 16 bits in Python memory - [ ] Base64 encrypts the payload using 64-bit DES keys --- ### Practical Challenge 🎯 Implement `b64_encode(data: bytes) -> str` from scratch using bitwise shift operators (`<<`, `>>`, `&`) and the 64-character table. Pad incomplete 3-byte chunks with `'='`. Test with `b'Python 3.13 Stream'` and `b'M'`, assert exact match against standard Base64 encoding (`'UHl0aG9uIDMuMTMgU3RyZWFt'` and `'TQ=='`), and print results. <details> <summary>Solution</summary> ```python B64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" def b64_encode(data: bytes) -> str: res = [] i = 0 n = len(data) while i < n: b0 = data[i] b1 = data[i + 1] if i + 1 < n else 0 b2 = data[i + 2] if i + 2 < n else 0 triple = (b0 << 16) | (b1 << 8) | b2 res.append(B64_CHARS[(triple >> 18) & 0x3F]) res.append(B64_CHARS[(triple >> 12) & 0x3F]) res.append(B64_CHARS[(triple >> 6) & 0x3F] if i + 1 < n else "=") res.append(B64_CHARS[triple & 0x3F] if i + 2 < n else "=") i += 3 return "".join(res) test1 = b"Python 3.13 Stream" test2 = b"M" enc1 = b64_encode(test1) enc2 = b64_encode(test2) assert enc1 == "UHl0aG9uIDMuMTMgU3RyZWFt" assert enc2 == "TQ==" print(f"Base64 bitwise encoder verified: 'M' -> '{enc2}', 'Python...' -> '{enc1}'") ``` </details>