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.
This commit is contained in:
Zooko O'Whielacronx 2007-03-29 18:11:30 -07:00
parent 4b4f5bbcba
commit 99a046ab51

View File

@ -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()