From 8117269181e9e3a38d149eb7341cd465b83c3351 Mon Sep 17 00:00:00 2001 From: Saiyam Jain Date: Mon, 26 Oct 2026 21:01:54 +0530 Subject: [PATCH] fix(ciphers): operate on raw bytes in base32 to fix corruption and empty encoding --- ciphers/base32.py | 57 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/ciphers/base32.py b/ciphers/base32.py index 911afa2452c0..3938623d8617 100644 --- a/ciphers/base32.py +++ b/ciphers/base32.py @@ -2,6 +2,7 @@ Base32 encoding and decoding https://en.wikipedia.org/wiki/Base32 +https://www.rfc-editor.org/rfc/rfc4648.html#section-6 """ B32_CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" @@ -9,35 +10,75 @@ def base32_encode(data: bytes) -> bytes: """ + Encode bytes into Base32 bytes per RFC 4648. + + >>> base32_encode(b"") + b'' >>> base32_encode(b"Hello World!") b'JBSWY3DPEBLW64TMMQQQ====' >>> base32_encode(b"123456") b'GEZDGNBVGY======' >>> base32_encode(b"some long complex string") b'ONXW2ZJANRXW4ZZAMNXW24DMMV4CA43UOJUW4ZY=' + >>> base32_encode(b"\\xff\\xfe\\x01") + b'777AC===' + >>> import base64 + >>> all( + ... base32_encode(bytes([i])) == base64.b32encode(bytes([i])) + ... for i in range(256) + ... ) + True """ - binary_data = "".join(bin(ord(d))[2:].zfill(8) for d in data.decode("utf-8")) - binary_data = binary_data.ljust(5 * ((len(binary_data) // 5) + 1), "0") - b32_chunks = map("".join, zip(*[iter(binary_data)] * 5)) + if not isinstance(data, (bytes, bytearray)): + msg = f"data must be bytes or bytearray, not {type(data).__name__}" + raise TypeError(msg) + + if not data: + return b"" + + binary_data = "".join(bin(byte)[2:].zfill(8) for byte in data) + pad_len = (5 - len(binary_data) % 5) % 5 + binary_data += "0" * pad_len + b32_chunks = [binary_data[i : i + 5] for i in range(0, len(binary_data), 5)] b32_result = "".join(B32_CHARSET[int(chunk, 2)] for chunk in b32_chunks) - return bytes(b32_result.ljust(8 * ((len(b32_result) // 8) + 1), "="), "utf-8") + pad_chars = (8 - len(b32_result) % 8) % 8 + return (b32_result + "=" * pad_chars).encode("ascii") def base32_decode(data: bytes) -> bytes: """ + Decode Base32 bytes back into raw bytes. + + >>> base32_decode(b"") + b'' >>> base32_decode(b'JBSWY3DPEBLW64TMMQQQ====') b'Hello World!' >>> base32_decode(b'GEZDGNBVGY======') b'123456' >>> base32_decode(b'ONXW2ZJANRXW4ZZAMNXW24DMMV4CA43UOJUW4ZY=') b'some long complex string' + >>> base32_decode(b'777AC===') + b'\\xff\\xfe\\x01' + >>> import base64 + >>> all( + ... base32_decode(base64.b32encode(bytes([i]))) == bytes([i]) + ... for i in range(256) + ... ) + True """ + if not isinstance(data, (bytes, bytearray)): + msg = f"data must be bytes or bytearray, not {type(data).__name__}" + raise TypeError(msg) + + cleaned = data.decode("ascii").rstrip("=") + if not cleaned: + return b"" + binary_chunks = "".join( - bin(B32_CHARSET.index(_d))[2:].zfill(5) - for _d in data.decode("utf-8").strip("=") + bin(B32_CHARSET.index(char))[2:].zfill(5) for char in cleaned ) - binary_data = list(map("".join, zip(*[iter(binary_chunks)] * 8))) - return bytes("".join([chr(int(_d, 2)) for _d in binary_data]), "utf-8") + num_bytes = len(binary_chunks) // 8 + return bytes(int(binary_chunks[i * 8 : (i + 1) * 8], 2) for i in range(num_bytes)) if __name__ == "__main__":