From 8d3edcf6641fa3b1f76030acef98a2825e73e2c1 Mon Sep 17 00:00:00 2001 From: QuartzAl <55610038+QuartzAl@users.noreply.github.com> Date: Wed, 20 Oct 2021 13:00:25 +0700 Subject: [PATCH 1/3] split into usable functions and added docstrings --- ciphers/base32.py | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/ciphers/base32.py b/ciphers/base32.py index da289a7210e8..a8f5fdfbdaa0 100644 --- a/ciphers/base32.py +++ b/ciphers/base32.py @@ -1,13 +1,46 @@ import base64 -def main() -> None: - inp = input("->") - encoded = inp.encode("utf-8") # encoded the input (we need a bytes like object) - b32encoded = base64.b32encode(encoded) # b32encoded the encoded string - print(b32encoded) - print(base64.b32decode(b32encoded).decode("utf-8")) # decoded it +def base32_encode(string: str) -> bytes: + """ + Encodes a given string to base32, returning a bytes-like object + >>> base32_encode("Hello World!") + b'JBSWY3DPEBLW64TMMQQQ====' + >>> base32_encode("123456") + b'GEZDGNBVGY======' + >>> base32_encode("some long complex string") + b'ONXW2ZJANRXW4ZZAMNXW24DMMV4CA43UOJUW4ZY=' + """ + + # encoded the input (we need a bytes like object) + string_bytes = string.encode("utf-8") + # b32encoded the bytes-like object + b32_encoded = base64.b32encode(string_bytes) + return b32_encoded + + +def base32_decode(encoded_bytes: bytes) -> str: + """ + Decodes a given bytes-like object to a string, returning a string + >>> base32_decode(b'JBSWY3DPEBLW64TMMQQQ====') + 'Hello World!' + >>> base32_decode(b'GEZDGNBVGY======') + '123456' + >>> base32_decode(b'ONXW2ZJANRXW4ZZAMNXW24DMMV4CA43UOJUW4ZY=') + 'some long complex string' + """ + + # decode the bytes from base32 + decoded_bytes = base64.b32decode(encoded_bytes) + # decode the bytes-like object to return as a string + decoded_string = decoded_bytes.decode("utf-8") + return decoded_string if __name__ == "__main__": - main() + test = "Hello World!" + encoded = base32_encode(test) + print(encoded) + + decoded = base32_decode(encoded) + print(decoded) From fcd6d5537457ee5a2633e1082e236010280e894b Mon Sep 17 00:00:00 2001 From: QuartzAl <55610038+QuartzAl@users.noreply.github.com> Date: Wed, 20 Oct 2021 21:10:17 +0700 Subject: [PATCH 2/3] Simplified code Co-authored-by: Christian Clauss --- ciphers/base32.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ciphers/base32.py b/ciphers/base32.py index a8f5fdfbdaa0..1452fc80a50f 100644 --- a/ciphers/base32.py +++ b/ciphers/base32.py @@ -31,10 +31,8 @@ def base32_decode(encoded_bytes: bytes) -> str: """ # decode the bytes from base32 - decoded_bytes = base64.b32decode(encoded_bytes) - # decode the bytes-like object to return as a string - decoded_string = decoded_bytes.decode("utf-8") - return decoded_string + # then, decode the bytes-like object to return as a string + return base64.b32decode(encoded_bytes).decode("utf-8") if __name__ == "__main__": From 68307950ccbdc7f3045026893c047f647f0ec434 Mon Sep 17 00:00:00 2001 From: QuartzAl <55610038+QuartzAl@users.noreply.github.com> Date: Wed, 20 Oct 2021 21:10:36 +0700 Subject: [PATCH 3/3] Simplified code Co-authored-by: Christian Clauss --- ciphers/base32.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ciphers/base32.py b/ciphers/base32.py index 1452fc80a50f..fee53ccaf0c4 100644 --- a/ciphers/base32.py +++ b/ciphers/base32.py @@ -13,10 +13,8 @@ def base32_encode(string: str) -> bytes: """ # encoded the input (we need a bytes like object) - string_bytes = string.encode("utf-8") - # b32encoded the bytes-like object - b32_encoded = base64.b32encode(string_bytes) - return b32_encoded + # then, b32encoded the bytes-like object + return base64.b32encode(string.encode("utf-8")) def base32_decode(encoded_bytes: bytes) -> str: