Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions ciphers/base85.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,55 @@
import base64
"""
Base85 (Ascii85) encoding and decoding

https://en.wikipedia.org/wiki/Ascii85
"""

def base85_encode(string: str) -> bytes:

def _base10_to_85(d: int) -> str:
return "".join(chr(d % 85 + 33)) + _base10_to_85(d // 85) if d > 0 else ""


def _base85_to_10(digits: list) -> int:
return sum(char * 85**i for i, char in enumerate(reversed(digits)))


def ascii85_encode(data: bytes) -> bytes:
"""
>>> base85_encode("")
>>> ascii85_encode(b"")
b''
>>> base85_encode("12345")
>>> ascii85_encode(b"12345")
b'0etOA2#'
>>> base85_encode("base 85")
>>> ascii85_encode(b"base 85")
b'@UX=h+?24'
"""
# encoded the input to a bytes-like object and then a85encode that
return base64.a85encode(string.encode("utf-8"))
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))]
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")


def base85_decode(a85encoded: bytes) -> str:
def ascii85_decode(data: bytes) -> bytes:
"""
>>> base85_decode(b"")
''
>>> base85_decode(b"0etOA2#")
'12345'
>>> base85_decode(b"@UX=h+?24")
'base 85'
>>> ascii85_decode(b"")
b''
>>> ascii85_decode(b"0etOA2#")
b'12345'
>>> ascii85_decode(b"@UX=h+?24")
b'base 85'
"""
# a85decode the input into bytes and decode that into a human readable string
return base64.a85decode(a85encoded).decode("utf-8")
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 __name__ == "__main__":
Expand Down