diff --git a/ciphers/base85.py b/ciphers/base85.py index f0228e5052dd..0dd897dfbfe0 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,20 @@ 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 - ] - result = "".join("".join(char) for char in char_chunks) - offset = int(null_values % 5 == 0) - return bytes(result[: offset - null_values], "utf-8") + 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] + 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__":