From 7e6c9c426f3cdaaed6a5bf650da144a03ff08501 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 24 Oct 2021 10:24:30 +0530 Subject: [PATCH 1/6] Add decode function --- ciphers/base16.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ciphers/base16.py b/ciphers/base16.py index f27ea4628e54..01ca9483fabb 100644 --- a/ciphers/base16.py +++ b/ciphers/base16.py @@ -4,6 +4,7 @@ def encode_to_b16(inp: str) -> bytes: """ Encodes a given utf-8 string into base-16. + >>> encode_to_b16('Hello World!') b'48656C6C6F20576F726C6421' >>> encode_to_b16('HELLO WORLD!') @@ -16,6 +17,21 @@ def encode_to_b16(inp: str) -> bytes: return b16encoded +def decode_from_b16(b16encoded: bytes) -> str: + """ + Decodes from base-16 to a utf-8 string. + + >>> decode_from_b16(b'48656C6C6F20576F726C6421') + 'Hello World!' + >>> decode_from_b16(b'48454C4C4F20574F524C4421') + 'HELLO WORLD!' + >>> decode_from_b16(b'') + '' + """ + encoded = base64.b16decode(b16encoded) #b16decoded the input + return encoded.decode('utf-8') # decoded the bytes like object to a string + + if __name__ == "__main__": import doctest From 5ecf28c57585a6a5ff9cb98822624933fdc604a0 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 24 Oct 2021 10:30:46 +0530 Subject: [PATCH 2/6] Update base16.py --- ciphers/base16.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ciphers/base16.py b/ciphers/base16.py index 01ca9483fabb..cf240ebbe84e 100644 --- a/ciphers/base16.py +++ b/ciphers/base16.py @@ -4,7 +4,7 @@ def encode_to_b16(inp: str) -> bytes: """ Encodes a given utf-8 string into base-16. - + >>> encode_to_b16('Hello World!') b'48656C6C6F20576F726C6421' >>> encode_to_b16('HELLO WORLD!') @@ -29,7 +29,7 @@ def decode_from_b16(b16encoded: bytes) -> str: '' """ encoded = base64.b16decode(b16encoded) #b16decoded the input - return encoded.decode('utf-8') # decoded the bytes like object to a string + return encoded.decode("utf-8") # decoded the bytes like object to a string if __name__ == "__main__": From 1c648b565198c1bf1fcf4db6aabc583cd49b6ed5 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 24 Oct 2021 10:34:25 +0530 Subject: [PATCH 3/6] Update base16.py --- ciphers/base16.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ciphers/base16.py b/ciphers/base16.py index cf240ebbe84e..1c48f1a4e840 100644 --- a/ciphers/base16.py +++ b/ciphers/base16.py @@ -28,7 +28,7 @@ def decode_from_b16(b16encoded: bytes) -> str: >>> decode_from_b16(b'') '' """ - encoded = base64.b16decode(b16encoded) #b16decoded the input + encoded = base64.b16decode(b16encoded) # b16decoded the input return encoded.decode("utf-8") # decoded the bytes like object to a string From 2447bd65847848ebd03d047e1217acfaadd9bb05 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Tue, 26 Oct 2021 18:37:55 +0530 Subject: [PATCH 4/6] Update base16.py --- ciphers/base16.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ciphers/base16.py b/ciphers/base16.py index 1c48f1a4e840..b35513646f96 100644 --- a/ciphers/base16.py +++ b/ciphers/base16.py @@ -12,9 +12,8 @@ def encode_to_b16(inp: str) -> bytes: >>> encode_to_b16('') b'' """ - encoded = inp.encode("utf-8") # encoded the input (we need a bytes like object) - b16encoded = base64.b16encode(encoded) # b16encoded the encoded string - return b16encoded + # encode the input into a bytes-like object and then encode b16encode the bytes + return base64.b16encode(inp.encode("utf-8")) def decode_from_b16(b16encoded: bytes) -> str: @@ -28,8 +27,8 @@ def decode_from_b16(b16encoded: bytes) -> str: >>> decode_from_b16(b'') '' """ - encoded = base64.b16decode(b16encoded) # b16decoded the input - return encoded.decode("utf-8") # decoded the bytes like object to a string + # b16decode the input into bytes and then decode the bytes into a human readable string + return base64.b16decode(b16encoded).decode("utf-8") if __name__ == "__main__": From 84b49560e77e0e998ab314c88f7898bb08f90eba Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Tue, 26 Oct 2021 18:42:05 +0530 Subject: [PATCH 5/6] Made the line shorter --- ciphers/base16.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ciphers/base16.py b/ciphers/base16.py index b35513646f96..d5ed20180b2d 100644 --- a/ciphers/base16.py +++ b/ciphers/base16.py @@ -12,7 +12,7 @@ def encode_to_b16(inp: str) -> bytes: >>> encode_to_b16('') b'' """ - # encode the input into a bytes-like object and then encode b16encode the bytes + # encode the input into a bytes-like object and then encode b16encode that return base64.b16encode(inp.encode("utf-8")) From e232bb3087f3a052b94fbed27bfdd12c0beda0b9 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Tue, 26 Oct 2021 18:46:35 +0530 Subject: [PATCH 6/6] Made another line shorter --- ciphers/base16.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ciphers/base16.py b/ciphers/base16.py index d5ed20180b2d..1ef60868dc3f 100644 --- a/ciphers/base16.py +++ b/ciphers/base16.py @@ -27,7 +27,7 @@ def decode_from_b16(b16encoded: bytes) -> str: >>> decode_from_b16(b'') '' """ - # b16decode the input into bytes and then decode the bytes into a human readable string + # b16decode the input into bytes and decode that into a human readable string return base64.b16decode(b16encoded).decode("utf-8")