-
Notifications
You must be signed in to change notification settings - Fork 0
/
adler32.py
69 lines (53 loc) · 1.44 KB
/
adler32.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
# coding: utf-8
from __future__ import division
# import sys, os, time, platform, math
# from struct import unpack, pack
# Copy from https://golang.org/src/hash/adler32/adler32.go
# // BASE is the largest prime that is less than 65536.
BASE = 65521;
# // nmax is the largest n such that
# // 255 * n * (n+1) / 2 + (n+1) * (BASE-1) <= 2^32-1.
# // It is mentioned in RFC 1950 (search for "5552").
NMAX = 5552;
U32_MAX_VALUE = 4294967295
U32_MIN_VALUE = 0
def u32(n):
if type(n) == float: n = int(n)
return min(max(n, U32_MIN_VALUE), U32_MAX_VALUE)
def update(d, p):
s1 = u32(d & 0xffff)
s2 = u32(d >> 16)
while len(p) > 0:
q = []
if len(p) > NMAX:
p, q = p[:NMAX], p[NMAX:]
while len(p) >= 4:
s1 += u32(p[0])
s2 += s1
s1 += u32(p[1])
s2 += s1
s1 += u32(p[2])
s2 += s1
s1 += u32(p[3])
s2 += s1
p = p[4:]
for x in p:
s1 += u32(x)
s2 += s1
s1 %= BASE
s2 %= BASE
p = q
return u32(s2 << 16 | s1)
def check_sum(data):
digest = 1
p = map(lambda s: ord(s), list(data))
return update(digest, p)
def main():
s = "hello, 世界!"
digest = check_sum(s)
hex_digest = hex(digest)
# 1096681671 (0x415e08c7)
print "D: %d Hex: %s" % (digest, hex_digest)
if __name__ == '__main__':
main()