From 99a046ab51b4e7d7cf66000bc5dd551a913a3fe2 Mon Sep 17 00:00:00 2001 From: Zooko O'Whielacronx Date: Thu, 29 Mar 2007 18:11:30 -0700 Subject: [PATCH] hashutil: convenience methods for tagged and encoded hashes In various cases, including Merkle Trees, it is useful to tag and encode the inputs to your secure hashes to prevent security flaws due to ambiguous meanings of hash values. --- src/allmydata/util/hashutil.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/allmydata/util/hashutil.py diff --git a/src/allmydata/util/hashutil.py b/src/allmydata/util/hashutil.py new file mode 100644 index 00000000..ccadccf0 --- /dev/null +++ b/src/allmydata/util/hashutil.py @@ -0,0 +1,18 @@ +from allmydata.Crypto.Hash import SHA256 + +def netstring(s): + return "%d:%s," % (len(s), s,) + +def tagged_hash(tag, val): + s = SHA256.new() + s.update(netstring(tag)) + s.update(val) + return s.digest() + +def tagged_pair_hash(tag, val1, val2): + s = SHA256.new() + s.update(netstring(tag)) + s.update(netstring(val1)) + s.update(netstring(val2)) + return s.digest() + -- 2.45.2