xref: /aosp_15_r20/external/brotli/scripts/dictionary/step-03-validate-bin.py (revision f4ee7fba7774faf2a30f13154332c0a06550dbc4)
1*f4ee7fbaSAndroid Build Coastguard Worker# Step 03 - validate raw dictionary file.
2*f4ee7fbaSAndroid Build Coastguard Worker#
3*f4ee7fbaSAndroid Build Coastguard Worker# CRC32, MD5, SHA1 and SHA256 checksums for raw binary dictionary are checked.
4*f4ee7fbaSAndroid Build Coastguard Worker
5*f4ee7fbaSAndroid Build Coastguard Workerimport hashlib
6*f4ee7fbaSAndroid Build Coastguard Workerimport zlib
7*f4ee7fbaSAndroid Build Coastguard Worker
8*f4ee7fbaSAndroid Build Coastguard Workerbin_path = "dictionary.bin"
9*f4ee7fbaSAndroid Build Coastguard Worker
10*f4ee7fbaSAndroid Build Coastguard Workerwith open(bin_path, "rb") as raw:
11*f4ee7fbaSAndroid Build Coastguard Worker  data = raw.read()
12*f4ee7fbaSAndroid Build Coastguard Worker
13*f4ee7fbaSAndroid Build Coastguard Workerdef check_digest(name, expected, actual):
14*f4ee7fbaSAndroid Build Coastguard Worker  if expected == actual:
15*f4ee7fbaSAndroid Build Coastguard Worker    print("[OK] " + name)
16*f4ee7fbaSAndroid Build Coastguard Worker  else:
17*f4ee7fbaSAndroid Build Coastguard Worker    print("[ERROR] " + name + " | " + expected + " != " + actual)
18*f4ee7fbaSAndroid Build Coastguard Worker
19*f4ee7fbaSAndroid Build Coastguard Worker
20*f4ee7fbaSAndroid Build Coastguard Workercheck_digest(
21*f4ee7fbaSAndroid Build Coastguard Worker    "CRC32",  # This is the only checksum provided in RFC.
22*f4ee7fbaSAndroid Build Coastguard Worker    "0x5136cb04",
23*f4ee7fbaSAndroid Build Coastguard Worker    hex(zlib.crc32(data)))
24*f4ee7fbaSAndroid Build Coastguard Worker
25*f4ee7fbaSAndroid Build Coastguard Workercheck_digest(
26*f4ee7fbaSAndroid Build Coastguard Worker    "MD5",
27*f4ee7fbaSAndroid Build Coastguard Worker    "96cecd2ee7a666d5aa3627d74735b32a",
28*f4ee7fbaSAndroid Build Coastguard Worker    hashlib.md5(data).hexdigest())
29*f4ee7fbaSAndroid Build Coastguard Worker
30*f4ee7fbaSAndroid Build Coastguard Workercheck_digest(
31*f4ee7fbaSAndroid Build Coastguard Worker    "SHA1",
32*f4ee7fbaSAndroid Build Coastguard Worker    "72b41051cb61a9281ba3c4414c289da50d9a7640",
33*f4ee7fbaSAndroid Build Coastguard Worker    hashlib.sha1(data).hexdigest())
34*f4ee7fbaSAndroid Build Coastguard Worker
35*f4ee7fbaSAndroid Build Coastguard Workercheck_digest(
36*f4ee7fbaSAndroid Build Coastguard Worker    "SHA256",
37*f4ee7fbaSAndroid Build Coastguard Worker    "20e42eb1b511c21806d4d227d07e5dd06877d8ce7b3a817f378f313653f35c70",
38*f4ee7fbaSAndroid Build Coastguard Worker    hashlib.sha256(data).hexdigest())
39