Produce new ETag when updating an object.
[pithos] / pithos / api / util.py
index 175e1cd..54a493e 100644 (file)
@@ -35,6 +35,7 @@ from functools import wraps
 from time import time
 from traceback import format_exc
 from wsgiref.handlers import format_date_time
+from binascii import hexlify
 
 from django.conf import settings
 from django.http import HttpResponse
@@ -427,6 +428,21 @@ class ObjectWrapper(object):
                 out.append('')
                 return '\r\n'.join(out)
 
+def hashmap_hash(hashmap):
+    """ Produce the root hash, treating the hashmap as a Merkle-like tree."""
+    
+    def subhash(d):
+        h = hashlib.new(backend.hash_algorithm)
+        h.update(d)
+        return h.digest()
+    
+    # TODO: Should create the whole tree and decide what to do with fillers.
+    h = hashmap
+    h = [subhash(h[x] + (h[x + 1] if x + 1 < len(h) else '')) for x in range(0, len(h), 2)]
+    while len(h) > 1:
+        h = [subhash(h[x] + (h[x + 1] if x + 1 < len(h) else '')) for x in range(0, len(h), 2)]
+    return hexlify(h[0])
+
 def update_response_headers(request, response):
     if request.serialization == 'xml':
         response['Content-Type'] = 'application/xml; charset=UTF-8'