Skip to content

Commit 288b0be

Browse files
bnMikheilistokhos
authored andcommitted
Hash adler32 (TheAlgorithms#2111)
* implement hash * fix indentation
1 parent c08bca1 commit 288b0be

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Diff for: hashes/adler32.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Adler-32 is a checksum algorithm which was invented by Mark Adler in 1995.
3+
Compared to a cyclic redundancy check of the same length, it trades reliability for speed (preferring the latter).
4+
Adler-32 is more reliable than Fletcher-16, and slightly less reliable than Fletcher-32.[2]
5+
6+
source: https://en.wikipedia.org/wiki/Adler-32
7+
"""
8+
9+
10+
def adler32(plain_text: str) -> str:
11+
"""
12+
Function implements adler-32 hash.
13+
Itterates and evaluates new value for each character
14+
15+
>>> adler32('Algorithms')
16+
363791387
17+
18+
>>> adler32('go adler em all')
19+
708642122
20+
"""
21+
MOD_ADLER = 65521
22+
a = 1
23+
b = 0
24+
for plain_chr in plain_text:
25+
a = (a + ord(plain_chr)) % MOD_ADLER
26+
b = (b + a) % MOD_ADLER
27+
return (b << 16) | a

0 commit comments

Comments
 (0)