MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / base32_encode

Function base32_encode

ciphers/base32.py:10–23  ·  view source on GitHub ↗

>>> base32_encode(b"Hello World!") b'JBSWY3DPEBLW64TMMQQQ====' >>> base32_encode(b"123456") b'GEZDGNBVGY======' >>> base32_encode(b"some long complex string") b'ONXW2ZJANRXW4ZZAMNXW24DMMV4CA43UOJUW4ZY='

(data: bytes)

Source from the content-addressed store, hash-verified

8
9
10def base32_encode(data: bytes) -> bytes:
11 """
12 >>> base32_encode(b"Hello World!")
13 b'JBSWY3DPEBLW64TMMQQQ===='
14 >>> base32_encode(b"123456")
15 b'GEZDGNBVGY======'
16 >>> base32_encode(b"some long complex string")
17 b'ONXW2ZJANRXW4ZZAMNXW24DMMV4CA43UOJUW4ZY='
18 """
19 binary_data = "".join(bin(ord(d))[2:].zfill(8) for d in data.decode("utf-8"))
20 binary_data = binary_data.ljust(5 * ((len(binary_data) // 5) + 1), "0")
21 b32_chunks = map("".join, zip(*[iter(binary_data)] * 5))
22 b32_result = "".join(B32_CHARSET[int(chunk, 2)] for chunk in b32_chunks)
23 return bytes(b32_result.ljust(8 * ((len(b32_result) // 8) + 1), "="), "utf-8")
24
25
26def base32_decode(data: bytes) -> bytes:

Callers

nothing calls this directly

Calls 1

decodeMethod · 0.45

Tested by

no test coverage detected