2015-05-06 06:46:02 -04:00
|
|
|
import argparse
|
|
|
|
import hashlib
|
|
|
|
import json
|
2018-10-19 20:16:55 -04:00
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from unpaddedbase64 import encode_base64
|
|
|
|
|
|
|
|
from synapse.crypto.event_signing import (
|
|
|
|
check_event_content_hash,
|
|
|
|
compute_event_reference_hash,
|
|
|
|
)
|
2015-05-06 06:46:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
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()
|
2018-10-19 20:16:55 -04:00
|
|
|
parser.add_argument(
|
|
|
|
"input_json", nargs="?", type=argparse.FileType('r'), default=sys.stdin
|
|
|
|
)
|
2015-05-06 06:46:02 -04:00
|
|
|
args = parser.parse_args()
|
|
|
|
logging.basicConfig()
|
|
|
|
|
|
|
|
event_json = dictobj(json.load(args.input_json))
|
|
|
|
|
2018-10-19 20:16:55 -04:00
|
|
|
algorithms = {"sha256": hashlib.sha256}
|
2015-05-06 06:46:02 -04:00
|
|
|
|
|
|
|
for alg_name in event_json.hashes:
|
|
|
|
if check_event_content_hash(event_json, algorithms[alg_name]):
|
2018-10-19 20:16:55 -04:00
|
|
|
print("PASS content hash %s" % (alg_name,))
|
2015-05-06 06:46:02 -04:00
|
|
|
else:
|
2018-10-19 20:16:55 -04:00
|
|
|
print("FAIL content hash %s" % (alg_name,))
|
2015-05-06 06:46:02 -04:00
|
|
|
|
|
|
|
for algorithm in algorithms.values():
|
|
|
|
name, h_bytes = compute_event_reference_hash(event_json, algorithm)
|
2018-10-19 20:16:55 -04:00
|
|
|
print("Reference hash %s: %s" % (name, encode_base64(h_bytes)))
|
2015-05-06 06:46:02 -04:00
|
|
|
|
|
|
|
|
2018-10-19 20:16:55 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|