Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split base85.py into functions, Add doctests #5746

Merged
merged 4 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions ciphers/base16.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
34 changes: 27 additions & 7 deletions ciphers/base85.py
Original file line number Diff line number Diff line change
@@ -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()