G
YouTube TikTok Games
Skip to content

fix: encode input as UTF-8 bytes in adler32 to match the Adler-32 spec - #15706

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

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

Conversation

@Ankushkushwaha1

Copy link
Copy Markdown

Description

Fixes #15635

The Adler-32 algorithm is defined over a byte stream. The previous implementation iterated over Unicode code points via ord(char), which produced correct results only for pure-ASCII input. For any non-ASCII character (e.g. '€', '日本語') the checksum silently diverged from every standard Adler-32 implementation, including Python's own zlib.adler32.

Root Cause

# Before (wrong for non-ASCII)
for plain_chr in plain_text:
    a = (a + ord(plain_chr)) % MOD_ADLER  # uses Unicode code point ❌

Fix

Encode the input string to UTF-8 first, then iterate over the resulting bytes — exactly what hashes/crc32.py already does:

# After (correct for all inputs)
for byte in plain_text.encode("utf-8"):
    a = (a + byte) % MOD_ADLER  # uses actual bytes ✅

Verification

>>> from hashes.adler32 import adler32
>>> import zlib
>>> adler32('Algorithms') == zlib.adler32('Algorithms'.encode())
True
>>> adler32('€100') == zlib.adler32('€100'.encode())
True
>>> adler32('日本語テスト') == zlib.adler32('日本語テスト'.encode())
True

Checklist

  • All existing doctests still pass ('Algorithms' → 363791387, 'go adler em all' → 708642122)
  • Two new doctests added for non-ASCII inputs, verified against zlib.adler32
  • No new dependencies introduced

…lgorithms#15635)

The Adler-32 algorithm is defined over a byte stream. The previous
implementation iterated Unicode code points via ord(char), which
produced correct results only for pure-ASCII input.  For any non-ASCII
character (e.g. '€', '日') the checksum diverged from every standard
Adler-32 implementation including Python's own zlib.adler32.

Fix: encode the input string as UTF-8 first and iterate over the
resulting bytes, mirroring what hashes/crc32.py already does.
Added two new doctests with non-ASCII strings that verify the output
matches zlib.adler32.
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Nov 11, 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)

1 participant