Skip to content

Fix Adler-32 for non-ASCII text - #15665

Open
Lexus-099 wants to merge 2 commits into
TheAlgorithms:masterfrom
Lexus-099:fix-adler32-unicode-checksum-pr2
Open

Lexus-099 wants to merge 2 commits into
TheAlgorithms:masterfrom
Lexus-099:fix-adler32-unicode-checksum-pr2

Conversation

@Lexus-099

Copy link
Copy Markdown

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests?
  • Documentation change?

Checklist

  • I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/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".

Summary

  • Calculate Adler-32 using UTF-8 bytes instead of Unicode code points.
  • Preserve existing results for ASCII inputs.

Fixes #15635

Testing

  • The full repository test suite has not been run.
  • This change was prepared with AI assistance and should be reviewed before merging.

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

Copy link
Copy Markdown
Author

Hi! Thank you for reviewing my PR. I noticed that it is still waiting for a required review. Could a maintainer please review my changes and approve the pending workflow runs? Thank you!

@jgohel9902 jgohel9902 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for the fix. The UTF-8 approach is correct and backward compatible: I compared it with zlib.adler32(text.encode()) on several non-ASCII strings and everything matched. The main request is to add non-ASCII doctests so the bug stays fixed.

Comment thread hashes/adler32.py
Iterates and evaluates a new value for each character
Iterates over the UTF-8 bytes of the input text.

>>> adler32('Algorithms')

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The fix is correct: I compared it with zlib.adler32(text.encode()) on "€100", "héllo", "日本語" and an emoji, and all matched. But the doctests only cover ASCII, so nothing would catch this bug again. Please add non-ASCII doctests with the literal expected values, e.g.:

>>> adler32('€100')
196018850
>>> adler32('héllo')
192152348
>>> adler32('日本語')
556009089

You could also compare against zlib: >>> import zlib then >>> adler32('€100') == zlib.adler32('€100'.encode()), which shows the property being fixed.

Comment thread hashes/adler32.py
b = 0
for plain_chr in plain_text:
a = (a + ord(plain_chr)) % MOD_ADLER
for plain_byte in plain_text.encode("utf-8"):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good: encode() is evaluated once before the loop, and UTF-8 matches what zlib.adler32 gets when callers pass text.encode(). One suggestion: say in the docstring that the checksum is over the UTF-8 bytes, so non-ASCII results match zlib. That is the behaviour change, so documenting it helps. Line 17 already says "UTF-8 bytes", which is a good start.

Comment thread hashes/adler32.py
@@ -14,7 +14,7 @@
def adler32(plain_text: str) -> int:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional: there is another open PR for the same issue that changes the signature to bytes (this one keeps str and is backward compatible). The two overlap, so it may be worth coordinating with the maintainers, so only one is merged. This approach avoids breaking callers that pass strings, which I'd consider a plus.

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)

3 participants