Skip to content

fix(hashes): compute adler32 over UTF-8 bytes instead of code points - #15731

Open
TheSmurfs wants to merge 1 commit into
TheAlgorithms:masterfrom
TheSmurfs:fix/adler32-utf8-bytes
Open

TheSmurfs wants to merge 1 commit into
TheAlgorithms:masterfrom
TheSmurfs:fix/adler32-utf8-bytes

Conversation

@TheSmurfs

Copy link
Copy Markdown

Describe your change

  • Fix a bug or typo in an existing algorithm?

Fixes #15635

Adler-32 is defined over a byte stream, but hashes/adler32.py summed
Unicode code points, so any non-ASCII input produced a checksum that no
standard implementation would produce (e.g. adler32('€100') returned
2211914046 instead of 196018850).

This PR iterates the UTF-8 encoded bytes instead of the characters, so the
result now matches the reference implementation zlib.adler32(text.encode())
for every input, and aligns with the sibling module hashes/crc32.py
(which already takes bytes and matches zlib.crc32).

Doctests added: the non-ASCII examples from the issue, plus a cross-check
against zlib.adler32. Also verified locally with 500 randomized inputs
(ASCII, Latin-1, CJK, emoji) against zlib — all match. ruff check and
ruff format --check pass.

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 hashes/adler32.py: checksum diverges from the Adler-32 spec for any non-ASCII input (sums Unicode code points instead of bytes) #15635".

Adler-32 is defined over a byte stream, but the implementation summed
Unicode code points, so any non-ASCII input produced a checksum that no
standard implementation would produce (round-trip/verification broken).

Iterate the UTF-8 encoded bytes instead, matching zlib.adler32(text.encode())
for every input. Verified with doctests and 500 randomized cross-checks
against zlib.

Fixes TheAlgorithms#15635
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Nov 12, 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.

hashes/adler32.py: checksum diverges from the Adler-32 spec for any non-ASCII input (sums Unicode code points instead of bytes)

2 participants