diff options
author | Magnus Ahltorp <map@kth.se> | 2014-10-27 16:13:41 +0100 |
---|---|---|
committer | Magnus Ahltorp <map@kth.se> | 2014-10-27 16:13:41 +0100 |
commit | b9c709204da83be2f315664f9f263c6890b1bc8d (patch) | |
tree | c55d6f14a5eceb7aa038f3d558ddfa6d74c2461d /tools/certtools.py | |
parent | 44f48b0f96aba0009bd43036eea443f07cec71b9 (diff) |
fetchallcerts.py: calculate root hash
Diffstat (limited to 'tools/certtools.py')
-rw-r--r-- | tools/certtools.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/tools/certtools.py b/tools/certtools.py index 16c2105..b132caa 100644 --- a/tools/certtools.py +++ b/tools/certtools.py @@ -226,3 +226,27 @@ def timing_point(timer_dict=None, name=None): else: timer_dict = {"deltatimes":[], "lasttime":t} return timer_dict + +def internal_hash(pair): + if len(pair) == 1: + return pair[0] + else: + hash = hashlib.sha256() + hash.update(struct.pack(">b", 1)) + hash.update(pair[0]) + hash.update(pair[1]) + return hash.digest() + +def chunks(l, n): + return [l[i:i+n] for i in range(0, len(l), n)] + +def next_merkle_layer(layer): + return [internal_hash(pair) for pair in chunks(layer, 2)] + +def build_merkle_tree(layer0): + layers = [] + current_layer = layer0 + while len(current_layer) > 1: + current_layer = next_merkle_layer(current_layer) + layers.append(current_layer) + return layers |