1
0

check_event_hash.py 1.2 KB

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