1
0

check_event_hash.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import argparse
  2. import hashlib
  3. import json
  4. import logging
  5. import sys
  6. from unpaddedbase64 import encode_base64
  7. from synapse.crypto.event_signing import (
  8. check_event_content_hash,
  9. compute_event_reference_hash,
  10. )
  11. class dictobj(dict):
  12. def __init__(self, *args, **kargs):
  13. dict.__init__(self, *args, **kargs)
  14. self.__dict__ = self
  15. def get_dict(self):
  16. return dict(self)
  17. def get_full_dict(self):
  18. return dict(self)
  19. def get_pdu_json(self):
  20. return dict(self)
  21. def main():
  22. parser = argparse.ArgumentParser()
  23. parser.add_argument(
  24. "input_json", nargs="?", type=argparse.FileType('r'), default=sys.stdin
  25. )
  26. args = parser.parse_args()
  27. logging.basicConfig()
  28. event_json = dictobj(json.load(args.input_json))
  29. algorithms = {"sha256": hashlib.sha256}
  30. for alg_name in event_json.hashes:
  31. if check_event_content_hash(event_json, algorithms[alg_name]):
  32. print("PASS content hash %s" % (alg_name,))
  33. else:
  34. print("FAIL content hash %s" % (alg_name,))
  35. for algorithm in algorithms.values():
  36. name, h_bytes = compute_event_reference_hash(event_json, algorithm)
  37. print("Reference hash %s: %s" % (name, encode_base64(h_bytes)))
  38. if __name__ == "__main__":
  39. main()