Skip to content

Commit 5a77e02

Browse files
Merge branch 'TheAlgorithms:master' into main
2 parents 0e73eb9 + b2e186f commit 5a77e02

File tree

6 files changed

+645
-41
lines changed

6 files changed

+645
-41
lines changed

Diff for: .pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ repos:
1616
- id: auto-walrus
1717

1818
- repo: https://github.com/astral-sh/ruff-pre-commit
19-
rev: v0.0.288
19+
rev: v0.0.291
2020
hooks:
2121
- id: ruff
2222

Diff for: ciphers/base32.py

+27-24
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,45 @@
1-
import base64
1+
"""
2+
Base32 encoding and decoding
23
4+
https://en.wikipedia.org/wiki/Base32
5+
"""
6+
B32_CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
37

4-
def base32_encode(string: str) -> bytes:
8+
9+
def base32_encode(data: bytes) -> bytes:
510
"""
6-
Encodes a given string to base32, returning a bytes-like object
7-
>>> base32_encode("Hello World!")
11+
>>> base32_encode(b"Hello World!")
812
b'JBSWY3DPEBLW64TMMQQQ===='
9-
>>> base32_encode("123456")
13+
>>> base32_encode(b"123456")
1014
b'GEZDGNBVGY======'
11-
>>> base32_encode("some long complex string")
15+
>>> base32_encode(b"some long complex string")
1216
b'ONXW2ZJANRXW4ZZAMNXW24DMMV4CA43UOJUW4ZY='
1317
"""
14-
15-
# encoded the input (we need a bytes like object)
16-
# then, b32encoded the bytes-like object
17-
return base64.b32encode(string.encode("utf-8"))
18+
binary_data = "".join(bin(ord(d))[2:].zfill(8) for d in data.decode("utf-8"))
19+
binary_data = binary_data.ljust(5 * ((len(binary_data) // 5) + 1), "0")
20+
b32_chunks = map("".join, zip(*[iter(binary_data)] * 5))
21+
b32_result = "".join(B32_CHARSET[int(chunk, 2)] for chunk in b32_chunks)
22+
return bytes(b32_result.ljust(8 * ((len(b32_result) // 8) + 1), "="), "utf-8")
1823

1924

20-
def base32_decode(encoded_bytes: bytes) -> str:
25+
def base32_decode(data: bytes) -> bytes:
2126
"""
22-
Decodes a given bytes-like object to a string, returning a string
2327
>>> base32_decode(b'JBSWY3DPEBLW64TMMQQQ====')
24-
'Hello World!'
28+
b'Hello World!'
2529
>>> base32_decode(b'GEZDGNBVGY======')
26-
'123456'
30+
b'123456'
2731
>>> base32_decode(b'ONXW2ZJANRXW4ZZAMNXW24DMMV4CA43UOJUW4ZY=')
28-
'some long complex string'
32+
b'some long complex string'
2933
"""
30-
31-
# decode the bytes from base32
32-
# then, decode the bytes-like object to return as a string
33-
return base64.b32decode(encoded_bytes).decode("utf-8")
34+
binary_chunks = "".join(
35+
bin(B32_CHARSET.index(_d))[2:].zfill(5)
36+
for _d in data.decode("utf-8").strip("=")
37+
)
38+
binary_data = list(map("".join, zip(*[iter(binary_chunks)] * 8)))
39+
return bytes("".join([chr(int(_d, 2)) for _d in binary_data]), "utf-8")
3440

3541

3642
if __name__ == "__main__":
37-
test = "Hello World!"
38-
encoded = base32_encode(test)
39-
print(encoded)
43+
import doctest
4044

41-
decoded = base32_decode(encoded)
42-
print(decoded)
45+
doctest.testmod()

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)