Repository navigation
fix(ciphers): replace base58.py with a real Base58 implementation - #15638
Open
SakshiYeole wants to merge 1 commit into
Open
SakshiYeole wants to merge 1 commit into
SakshiYeole wants to merge 1 commit into
Conversation
ciphers/base58.py was a byte-identical copy of ciphers/base64_cipher.py, so the repository had no Base58 code. Replace it with an encoder and decoder for the Bitcoin alphabet: - base58_encode(bytes) -> bytes - base58_decode(str | bytes) -> bytes - each leading zero byte maps to a leading "1" and back - TypeError for wrong input types, ValueError for invalid characters The signatures mirror base64_cipher.py. Doctests cover empty input, leading zeros, a round trip, and the error paths. Fixes TheAlgorithms#15637 Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change
ciphers/base58.pywas a byte-identical copy ofciphers/base64_cipher.py, so the repository had no Base58 implementation (see #15637). This replaces it with a Base58 encoder/decoder using the Bitcoin alphabet.base58_encode(data: bytes) -> bytesbase58_decode(encoded_data: str | bytes) -> bytes1and restored on decode.TypeError; characters outside the alphabet (0,O,I,l, ...) raiseValueError.The signatures mirror
base64_cipher.py(bytesout,strorbytesaccepted by the decoder), so thebase*modules inciphers/stay consistent.Note on the issue's examples:
base58_encode(b"\x00\x01")is12(one leading zero byte gives one1, and the value 1 is2), and2zdecodes tob"s". The doctests use the verified values.Verification: doctests pass;
ruff check,ruff formatand pre-commit pass; 12,600 random inputs (including leading zero bytes) match the PyPIbase58package in both directions. I also ran the full test suite locally (headless): the only 2 failures areweb_programming/instagram_crawler.py(needs network) andmachine_learning/linear_discriminant_analysis.py::gaussian_distribution, which fail identically on a cleanupstream/master.Fixes #15637
Checklist