summary refs log tree commit diff
path: root/scripts-dev/check_event_hash.py
blob: cd5599e9a1ffd32045565facf2f3b646218a7b1b (plain) (blame)
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
import argparse
import hashlib
import json
import logging
import sys

from unpaddedbase64 import encode_base64

from synapse.crypto.event_signing import (
    check_event_content_hash,
    compute_event_reference_hash,
)


class dictobj(dict):
    def __init__(self, *args, **kargs):
        dict.__init__(self, *args, **kargs)
        self.__dict__ = self

    def get_dict(self):
        return dict(self)

    def get_full_dict(self):
        return dict(self)

    def get_pdu_json(self):
        return dict(self)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "input_json", nargs="?", type=argparse.FileType("r"), default=sys.stdin
    )
    args = parser.parse_args()
    logging.basicConfig()

    event_json = dictobj(json.load(args.input_json))

    algorithms = {"sha256": hashlib.sha256}

    for alg_name in event_json.hashes:
        if check_event_content_hash(event_json, algorithms[alg_name]):
            print("PASS content hash %s" % (alg_name,))
        else:
            print("FAIL content hash %s" % (alg_name,))

    for algorithm in algorithms.values():
        name, h_bytes = compute_event_reference_hash(event_json, algorithm)
        print("Reference hash %s: %s" % (name, encode_base64(h_bytes)))


if __name__ == "__main__":
    main()