From deab693df873d9fe9d68ce584b80c279ec70bfec Mon Sep 17 00:00:00 2001 From: "mr.p4rr0t" Date: Mon, 26 Oct 2026 18:49:36 +0300 Subject: [PATCH 1/2] fix(ciphers): operate on raw bytes in base85 --- ciphers/base85.py | 49 +++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/ciphers/base85.py b/ciphers/base85.py index f0228e5052dd..47c64da48c42 100644 --- a/ciphers/base85.py +++ b/ciphers/base85.py @@ -6,7 +6,11 @@ def _base10_to_85(d: int) -> str: - return "".join(chr(d % 85 + 33)) + _base10_to_85(d // 85) if d > 0 else "" + res = [] + for _ in range(5): + res.append(chr(d % 85 + 33)) + d //= 85 + return "".join(res) def _base85_to_10(digits: list) -> int: @@ -21,13 +25,22 @@ def ascii85_encode(data: bytes) -> bytes: b'0etOA2#' >>> ascii85_encode(b"base 85") b'@UX=h+?24' + >>> ascii85_encode("héllo".encode()) + b'BZ$fcCi9' """ - binary_data = "".join(bin(ord(d))[2:].zfill(8) for d in data.decode("utf-8")) - null_values = (32 * ((len(binary_data) // 32) + 1) - len(binary_data)) // 8 - binary_data = binary_data.ljust(32 * ((len(binary_data) // 32) + 1), "0") - b85_chunks = [int(_s, 2) for _s in map("".join, zip(*[iter(binary_data)] * 32))] + if not data: + return b"" + binary_data = "".join(bin(byte)[2:].zfill(8) for byte in data) + remainder = len(binary_data) % 32 + null_bits = (32 - remainder) % 32 + binary_data = binary_data + "0" * null_bits + null_bytes = null_bits // 8 + + b85_chunks = [ + int(binary_data[i : i + 32], 2) for i in range(0, len(binary_data), 32) + ] result = "".join(_base10_to_85(chunk)[::-1] for chunk in b85_chunks) - return bytes(result[:-null_values] if null_values % 4 != 0 else result, "utf-8") + return bytes(result[:-null_bytes] if null_bytes > 0 else result, "utf-8") def ascii85_decode(data: bytes) -> bytes: @@ -38,18 +51,22 @@ def ascii85_decode(data: bytes) -> bytes: b'12345' >>> ascii85_decode(b"@UX=h+?24") b'base 85' + >>> ascii85_decode(b'BZ$fcCi9').decode() + 'héllo' """ - null_values = 5 * ((len(data) // 5) + 1) - len(data) - binary_data = data.decode("utf-8") + "u" * null_values - b85_chunks = map("".join, zip(*[iter(binary_data)] * 5)) - b85_segments = [[ord(_s) - 33 for _s in chunk] for chunk in b85_chunks] - results = [bin(_base85_to_10(chunk))[2::].zfill(32) for chunk in b85_segments] - char_chunks = [ - [chr(int(_s, 2)) for _s in map("".join, zip(*[iter(r)] * 8))] for r in results + if not data: + return b"" + s = data.decode("ascii") + null_values = (5 - (len(s) % 5)) % 5 + binary_data = s + "u" * null_values + b85_chunks = [binary_data[i : i + 5] for i in range(0, len(binary_data), 5)] + b85_segments = [[ord(c) - 33 for c in chunk] for chunk in b85_chunks] + results = [bin(_base85_to_10(chunk))[2:].zfill(32) for chunk in b85_segments] + byte_chunks = [ + [int(r[j : j + 8], 2) for j in range(0, 32, 8)] for r in results ] - result = "".join("".join(char) for char in char_chunks) - offset = int(null_values % 5 == 0) - return bytes(result[: offset - null_values], "utf-8") + raw_bytes = bytes(b for chunk in byte_chunks for b in chunk) + return raw_bytes[:-null_values] if null_values > 0 else raw_bytes if __name__ == "__main__": From 9cbd732b3730df720b1e66be326e241e7c757dad 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:50:44 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ciphers/base85.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ciphers/base85.py b/ciphers/base85.py index 47c64da48c42..0dd897dfbfe0 100644 --- a/ciphers/base85.py +++ b/ciphers/base85.py @@ -62,9 +62,7 @@ def ascii85_decode(data: bytes) -> bytes: b85_chunks = [binary_data[i : i + 5] for i in range(0, len(binary_data), 5)] b85_segments = [[ord(c) - 33 for c in chunk] for chunk in b85_chunks] results = [bin(_base85_to_10(chunk))[2:].zfill(32) for chunk in b85_segments] - byte_chunks = [ - [int(r[j : j + 8], 2) for j in range(0, 32, 8)] for r in results - ] + byte_chunks = [[int(r[j : j + 8], 2) for j in range(0, 32, 8)] for r in results] raw_bytes = bytes(b for chunk in byte_chunks for b in chunk) return raw_bytes[:-null_values] if null_values > 0 else raw_bytes