-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Updated details to run the program #12121
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
Changes from 3 commits
35eb46f
e641f3d
8f5909e
f1041a1
e476d58
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" | ||
|
||
|
||
def base58_encode(data: bytes) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
"""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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Please provide descriptive name for the parameter: |
||
"""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() |
There was a problem hiding this comment.
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 functionbase58_encode