-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_parts_test.py
78 lines (67 loc) · 2.03 KB
/
hash_parts_test.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
70
71
72
73
74
75
76
77
78
import base64
import crc32c # note this is a 3d party module
import hashlib
import zlib
# test date to hash
data = "Hello, World!"
# read string s in chunks of up to n characters
def chunks(s, n):
lst = list(s)
for i in range(0, len(lst), n):
yield lst[i:i+n]
# hash b using MD5 and return the digest
def as_md5(b):
h = hashlib.md5()
h.update(b)
return h.digest()
# hash b using CRC32 (IEEE 802.3) and return the digest
def as_crc32(b):
x = zlib.crc32(b, 0)
return x.to_bytes(length=(x.bit_length() + 7) // 8, byteorder='big')
# hash b using CRC32C (Castagnoli) and return the digest
def as_crc32c(b):
x = crc32c.crc32c(b)
return x.to_bytes(length=(x.bit_length() + 7) // 8, byteorder='big')
# hash b using SHA1 and return the digest
def as_sha1(b):
h = hashlib.sha1()
h.update(b)
return h.digest()
# hash b using SHA256 and return the digest
def as_sha256(b):
h = hashlib.sha256()
h.update(b)
return h.digest()
# list of algorithms to produce test data for
algos = {
'MD5': as_md5,
'CRC32': as_crc32,
'CRC32C': as_crc32c,
'SHA1': as_sha1,
'SHA256': as_sha256,
}
# for each algos entry print out a test suite for TestNewHashParts in
# hash_parts_test.go
print('// test date generated by hash_parts_test.py')
print('var testHashPartsExpected = []struct {')
print('\tName string')
print('\tID *ChecksumAlgorithm')
print('\tData string')
print('\tBase64 map[int][]string')
print('}{')
for k, fn in algos.items():
print('\t{')
print(f'\t\tName: "ChecksumAlgorithm{k}",')
print(f'\t\tID: ChecksumAlgorithm{k},')
print(f'\t\tData: "{data}",')
print(f'\t\tBase64: map[int][]string{{')
for i in range(1, 14, 1):
print(f'\t\t\t{i}: []string{{')
for chunk in chunks(data, i):
b = "".join(chunk).encode('utf-8')
h = base64.b64encode(fn(b)).decode("utf-8")
print(f'\t\t\t\t"{h}", // {b.decode("utf-8").replace(" ", "<space>")}')
print('\t\t\t},')
print('\t\t},')
print('\t},')
print('}')