Skip to content

Commit 882fb2f

Browse files
Rewrite of base85.py algorithm (TheAlgorithms#9069)
* rewrite of base85.py * changed maps to list comprehension * Apply suggestions from code review Co-authored-by: Tianyi Zheng <[email protected]> --------- Co-authored-by: Tianyi Zheng <[email protected]>
1 parent 708d906 commit 882fb2f

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

Diff for: ciphers/base85.py

+41-16
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,55 @@
1-
import base64
1+
"""
2+
Base85 (Ascii85) encoding and decoding
23
4+
https://en.wikipedia.org/wiki/Ascii85
5+
"""
36

4-
def base85_encode(string: str) -> bytes:
7+
8+
def _base10_to_85(d: int) -> str:
9+
return "".join(chr(d % 85 + 33)) + _base10_to_85(d // 85) if d > 0 else ""
10+
11+
12+
def _base85_to_10(digits: list) -> int:
13+
return sum(char * 85**i for i, char in enumerate(reversed(digits)))
14+
15+
16+
def ascii85_encode(data: bytes) -> bytes:
517
"""
6-
>>> base85_encode("")
18+
>>> ascii85_encode(b"")
719
b''
8-
>>> base85_encode("12345")
20+
>>> ascii85_encode(b"12345")
921
b'0etOA2#'
10-
>>> base85_encode("base 85")
22+
>>> ascii85_encode(b"base 85")
1123
b'@UX=h+?24'
1224
"""
13-
# encoded the input to a bytes-like object and then a85encode that
14-
return base64.a85encode(string.encode("utf-8"))
25+
binary_data = "".join(bin(ord(d))[2:].zfill(8) for d in data.decode("utf-8"))
26+
null_values = (32 * ((len(binary_data) // 32) + 1) - len(binary_data)) // 8
27+
binary_data = binary_data.ljust(32 * ((len(binary_data) // 32) + 1), "0")
28+
b85_chunks = [int(_s, 2) for _s in map("".join, zip(*[iter(binary_data)] * 32))]
29+
result = "".join(_base10_to_85(chunk)[::-1] for chunk in b85_chunks)
30+
return bytes(result[:-null_values] if null_values % 4 != 0 else result, "utf-8")
1531

1632

17-
def base85_decode(a85encoded: bytes) -> str:
33+
def ascii85_decode(data: bytes) -> bytes:
1834
"""
19-
>>> base85_decode(b"")
20-
''
21-
>>> base85_decode(b"0etOA2#")
22-
'12345'
23-
>>> base85_decode(b"@UX=h+?24")
24-
'base 85'
35+
>>> ascii85_decode(b"")
36+
b''
37+
>>> ascii85_decode(b"0etOA2#")
38+
b'12345'
39+
>>> ascii85_decode(b"@UX=h+?24")
40+
b'base 85'
2541
"""
26-
# a85decode the input into bytes and decode that into a human readable string
27-
return base64.a85decode(a85encoded).decode("utf-8")
42+
null_values = 5 * ((len(data) // 5) + 1) - len(data)
43+
binary_data = data.decode("utf-8") + "u" * null_values
44+
b85_chunks = map("".join, zip(*[iter(binary_data)] * 5))
45+
b85_segments = [[ord(_s) - 33 for _s in chunk] for chunk in b85_chunks]
46+
results = [bin(_base85_to_10(chunk))[2::].zfill(32) for chunk in b85_segments]
47+
char_chunks = [
48+
[chr(int(_s, 2)) for _s in map("".join, zip(*[iter(r)] * 8))] for r in results
49+
]
50+
result = "".join("".join(char) for char in char_chunks)
51+
offset = int(null_values % 5 == 0)
52+
return bytes(result[: offset - null_values], "utf-8")
2853

2954

3055
if __name__ == "__main__":

0 commit comments

Comments
 (0)