Skip to content

Commit aaa0566

Browse files
committed
fix: handle Unicode in Adler-32 checksum
1 parent 574d100 commit aaa0566

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

‎hashes/adler32.py‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,26 @@
1313

1414
def adler32(plain_text: str) -> int:
1515
"""
16-
Function implements adler-32 hash.
17-
Iterates and evaluates a new value for each character
16+
Return the Adler-32 checksum of a string encoded as UTF-8.
1817
1918
>>> adler32('Algorithms')
2019
363791387
2120
2221
>>> adler32('go adler em all')
2322
708642122
23+
24+
>>> adler32('€100')
25+
196018850
26+
27+
>>> adler32('日本語テスト')
28+
2053180537
29+
30+
>>> adler32('')
31+
1
2432
"""
2533
a = 1
2634
b = 0
27-
for plain_chr in plain_text:
28-
a = (a + ord(plain_chr)) % MOD_ADLER
35+
for plain_byte in plain_text.encode("utf-8"):
36+
a = (a + plain_byte) % MOD_ADLER
2937
b = (b + a) % MOD_ADLER
3038
return (b << 16) | a

0 commit comments

Comments
 (0)