From 3cba478461270e088431766a6c6e8766d044c11d Mon Sep 17 00:00:00 2001 From: "mr.p4rr0t" Date: Mon, 26 Oct 2026 18:48:15 +0300 Subject: [PATCH 1/2] fix(ciphers): operate on raw bytes in base32 --- ciphers/base32.py | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/ciphers/base32.py b/ciphers/base32.py index 911afa2452c0..7e857365b10e 100644 --- a/ciphers/base32.py +++ b/ciphers/base32.py @@ -9,35 +9,48 @@ def base32_encode(data: bytes) -> bytes: """ + >>> 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("héllo".encode()) + b'NDB2S3DMN4======' """ - 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 data: + return b"" + binary_data = "".join(bin(byte)[2:].zfill(8) for byte in data) + remainder = len(binary_data) % 5 + if remainder != 0: + binary_data = binary_data + "0" * (5 - remainder) + 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_len = (8 - (len(b32_result) % 8)) % 8 + return (b32_result + "=" * pad_len).encode("ascii") def base32_decode(data: bytes) -> 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'NDB2S3DMN4======').decode() + 'héllo' """ - binary_chunks = "".join( - bin(B32_CHARSET.index(_d))[2:].zfill(5) - for _d in data.decode("utf-8").strip("=") - ) - binary_data = list(map("".join, zip(*[iter(binary_chunks)] * 8))) - return bytes("".join([chr(int(_d, 2)) for _d in binary_data]), "utf-8") + if not data: + return b"" + s = data.decode("ascii").strip("=") + binary_chunks = "".join(bin(B32_CHARSET.index(c))[2:].zfill(5) for c in s) + num_bytes = len(binary_chunks) // 8 + return bytes(int(binary_chunks[i : i + 8], 2) for i in range(0, num_bytes * 8, 8)) if __name__ == "__main__": From a205d0f526dddb4b55cd6053e8dfc56735b81fd4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 26 Oct 2026 15:49:05 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ciphers/base32.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ciphers/base32.py b/ciphers/base32.py index 7e857365b10e..39e0ad1da31f 100644 --- a/ciphers/base32.py +++ b/ciphers/base32.py @@ -23,8 +23,7 @@ def base32_encode(data: bytes) -> bytes: if not data: return b"" binary_data = "".join(bin(byte)[2:].zfill(8) for byte in data) - remainder = len(binary_data) % 5 - if remainder != 0: + if (remainder := len(binary_data) % 5) != 0: binary_data = binary_data + "0" * (5 - remainder) 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)