Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
60 changes: 60 additions & 0 deletions ciphers/base58.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"


def base58_encode(data: bytes) -> str:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file ciphers/base58.py, please provide doctest for the function base58_encode

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file ciphers/base58.py, please provide doctest for the function base58_encode

"""Encodes data in Base58.

Args:
data (bytes): Data to be encoded.

Returns:
str: Base58 encoded string.
"""
num = int.from_bytes(data, "big")
encoded = ""
while num > 0:
num, rem = divmod(num, 58)
encoded = BASE58_ALPHABET[rem] + encoded

# Add '1' for each leading 0 byte
n = 0
for byte in data:
if byte == 0:
n += 1
else:
break
return "1" * n + encoded


def base58_decode(s: str) -> bytes:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file ciphers/base58.py, please provide doctest for the function base58_decode

Please provide descriptive name for the parameter: s

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file ciphers/base58.py, please provide doctest for the function base58_decode

Please provide descriptive name for the parameter: s

"""Decodes Base58 encoded string back to bytes.

Args:
s (str): Base58 encoded string.

Returns:
bytes: Decoded data.

Raises:
ValueError: If the string contains invalid Base58 characters.
"""
num = 0
for char in s:
if char not in BASE58_ALPHABET:
error_message = f"Invalid character '{char}' in Base58 string"
raise ValueError(error_message)
num *= 58
num += BASE58_ALPHABET.index(char)

# Convert the number to bytes
combined = num.to_bytes((num.bit_length() + 7) // 8, "big")

# Add leading zeros
num_leading_zeros = len(s) - len(s.lstrip("1"))
return b"\x00" * num_leading_zeros + combined


if __name__ == "__main__":
import doctest

doctest.testmod()
2 changes: 2 additions & 0 deletions ciphers/base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ def base64_decode(encoded_data: str) -> bytes:
return bytes(data)




if __name__ == "__main__":
import doctest

Expand Down