mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 23:11:09 +00:00
c6582b35bf
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com> Co-authored-by: Christian Clauss <cclauss@me.com>
31 lines
783 B
Python
31 lines
783 B
Python
"""
|
|
Adler-32 is a checksum algorithm which was invented by Mark Adler in 1995.
|
|
Compared to a cyclic redundancy check of the same length, it trades reliability for
|
|
speed (preferring the latter).
|
|
Adler-32 is more reliable than Fletcher-16, and slightly less reliable than
|
|
Fletcher-32.[2]
|
|
|
|
source: https://en.wikipedia.org/wiki/Adler-32
|
|
"""
|
|
|
|
MOD_ADLER = 65521
|
|
|
|
|
|
def adler32(plain_text: str) -> int:
|
|
"""
|
|
Function implements adler-32 hash.
|
|
Iterates and evaluates a new value for each character
|
|
|
|
>>> adler32('Algorithms')
|
|
363791387
|
|
|
|
>>> adler32('go adler em all')
|
|
708642122
|
|
"""
|
|
a = 1
|
|
b = 0
|
|
for plain_chr in plain_text:
|
|
a = (a + ord(plain_chr)) % MOD_ADLER
|
|
b = (b + a) % MOD_ADLER
|
|
return (b << 16) | a
|