From bdd135d40343daf047a00abc9a7360db192793d9 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Tue, 2 Nov 2021 15:40:25 +0530 Subject: [PATCH] Split base85.py into functions, Add doctests (#5746) * Update base16.py * Rename base64_encoding.py to base64.py * Split into functions, Add doctests * Update base16.py --- ciphers/base16.py | 16 +++++------ ciphers/{base64_encoding.py => base64.py} | 0 ciphers/base85.py | 34 ++++++++++++++++++----- 3 files changed, 35 insertions(+), 15 deletions(-) rename ciphers/{base64_encoding.py => base64.py} (100%) diff --git a/ciphers/base16.py b/ciphers/base16.py index 1ef60868d..a149a6d8c 100644 --- a/ciphers/base16.py +++ b/ciphers/base16.py @@ -1,30 +1,30 @@ import base64 -def encode_to_b16(inp: str) -> bytes: +def base16_encode(inp: str) -> bytes: """ Encodes a given utf-8 string into base-16. - >>> encode_to_b16('Hello World!') + >>> base16_encode('Hello World!') b'48656C6C6F20576F726C6421' - >>> encode_to_b16('HELLO WORLD!') + >>> base16_encode('HELLO WORLD!') b'48454C4C4F20574F524C4421' - >>> encode_to_b16('') + >>> base16_encode('') b'' """ # encode the input into a bytes-like object and then encode b16encode that return base64.b16encode(inp.encode("utf-8")) -def decode_from_b16(b16encoded: bytes) -> str: +def base16_decode(b16encoded: bytes) -> str: """ Decodes from base-16 to a utf-8 string. - >>> decode_from_b16(b'48656C6C6F20576F726C6421') + >>> base16_decode(b'48656C6C6F20576F726C6421') 'Hello World!' - >>> decode_from_b16(b'48454C4C4F20574F524C4421') + >>> base16_decode(b'48454C4C4F20574F524C4421') 'HELLO WORLD!' - >>> decode_from_b16(b'') + >>> base16_decode(b'') '' """ # b16decode the input into bytes and decode that into a human readable string diff --git a/ciphers/base64_encoding.py b/ciphers/base64.py similarity index 100% rename from ciphers/base64_encoding.py rename to ciphers/base64.py diff --git a/ciphers/base85.py b/ciphers/base85.py index 9740299b9..afd1aff79 100644 --- a/ciphers/base85.py +++ b/ciphers/base85.py @@ -1,13 +1,33 @@ import base64 -def main() -> None: - inp = input("->") - encoded = inp.encode("utf-8") # encoded the input (we need a bytes like object) - a85encoded = base64.a85encode(encoded) # a85encoded the encoded string - print(a85encoded) - print(base64.a85decode(a85encoded).decode("utf-8")) # decoded it +def base85_encode(string: str) -> bytes: + """ + >>> base85_encode("") + b'' + >>> base85_encode("12345") + b'0etOA2#' + >>> base85_encode("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")) + + +def base85_decode(a85encoded: bytes) -> str: + """ + >>> base85_decode(b"") + '' + >>> base85_decode(b"0etOA2#") + '12345' + >>> base85_decode(b"@UX=h+?24") + 'base 85' + """ + # a85decode the input into bytes and decode that into a human readable string + return base64.a85decode(a85encoded).decode("utf-8") if __name__ == "__main__": - main() + import doctest + + doctest.testmod()