Skip to content

fix(ciphers): operate on raw bytes in base85 to fix corruption and decode crashes - #15644

Open
saiyam-jain-dev wants to merge 1 commit into
TheAlgorithms:masterfrom
saiyam-jain-dev:fix-base85-raw-bytes
Open

saiyam-jain-dev wants to merge 1 commit into
TheAlgorithms:masterfrom
saiyam-jain-dev:fix-base85-raw-bytes

Conversation

@saiyam-jain-dev

Copy link
Copy Markdown

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Ascii85 (Base85) is designed to encode arbitrary binary byte streams. The previous implementation in ciphers/base85.py had several bugs:

  1. �scii85_encode called data.decode("utf-8") directly on raw byte inputs. Any binary payload containing bytes $\ge 128$ (e.g. �"\xff\xfe\xfd") crashed with UnicodeDecodeError.
  2. �scii85_decode reconstructed bytes by converting 8-bit values to Python chr(val) strings and encoding to UTF-8 (�ytes(result, "utf-8")). This corrupted any decoded byte $\ge 128$ into multi-byte UTF-8 sequences (e.g. byte \x80 turned into \xc2\x80, and \xff turned into \xc3\xbf).
  3. Out-of-range character validation and invalid length handling were missing.

This PR:

  • Refactors �scii85_encode and �scii85_decode to operate strictly on raw bytes and 32-bit big-endian chunks adhering to Ascii85 (RFC 1924 / Adobe Ascii85 specification).
  • Produces identical results to Python standard library �ase64.a85encode and �ase64.a85decode.
  • Validates Ascii85 character bounds ( \le \text{ord}(c) \le 117$) and detects invalid single-remainder lengths.
  • Adds comprehensive doctests verifying edge cases and validating all 256 single-byte payloads against standard library outputs.

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Oct 26, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant