]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
hashutil: convenience methods for tagged and encoded hashes
authorZooko O'Whielacronx <zooko@zooko.com>
Fri, 30 Mar 2007 01:11:30 +0000 (18:11 -0700)
committerZooko O'Whielacronx <zooko@zooko.com>
Fri, 30 Mar 2007 01:11:30 +0000 (18:11 -0700)
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 [new file with mode: 0644]

diff --git a/src/allmydata/util/hashutil.py b/src/allmydata/util/hashutil.py
new file mode 100644 (file)
index 0000000..ccadccf
--- /dev/null
@@ -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()
+