gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r25652 - gnunet-update/src/gnunet_update


From: gnunet
Subject: [GNUnet-SVN] r25652 - gnunet-update/src/gnunet_update
Date: Wed, 26 Dec 2012 12:59:15 +0100

Author: harsha
Date: 2012-12-26 12:59:15 +0100 (Wed, 26 Dec 2012)
New Revision: 25652

Modified:
   gnunet-update/src/gnunet_update/hashtree.py
   gnunet-update/src/gnunet_update/util.py
Log:
- working gnunet-style CHK tree computation

Modified: gnunet-update/src/gnunet_update/hashtree.py
===================================================================
--- gnunet-update/src/gnunet_update/hashtree.py 2012-12-26 09:08:00 UTC (rev 
25651)
+++ gnunet-update/src/gnunet_update/hashtree.py 2012-12-26 11:59:15 UTC (rev 
25652)
@@ -20,9 +20,10 @@
 # Author:  Sree Harsha Totakura
 
 from hashlib import sha512
-from Crypto.Cipher import AES
 import logging
 
+import util
+
 # Defaults
 DBLOCK_SIZE = (32 * 1024)   # Data block size
 
@@ -32,11 +33,17 @@
 # byte = 2 * 512 bits).  DO NOT CHANGE!
 CHK_PER_INODE = 256
 
+CHK_HASH_SIZE = 64              # SHA-512 hash = 512 bits = 32 bytes
+
+CHK_QUERY_SIZE = CHK_HASH_SIZE  # Again a SHA-512 hash
+
 class Chk:
     key = None
     query = None
 
     def __init__(self, key, query):
+        assert (len(key) == CHK_HASH_SIZE)
+        assert (len(query) == CHK_QUERY_SIZE)
         self.key = key
         self.query = query
 
@@ -84,7 +91,7 @@
     bds = compute_tree_size_(depth)
     if (depth > 0):
         end_offset -= 1
-    ret = end_ofset / bds
+    ret = end_offset / bds
     return ret % CHK_PER_INODE
 
 def compute_iblock_size_(depth, offset):
@@ -107,7 +114,7 @@
         ret = CHK_PER_INODE
     else:
         bds /= CHK_PER_INODE
-        ret = mode / bds
+        ret = mod / bds
         if (mod % bds) is not 0:
             ret += 1
     return ret
@@ -130,17 +137,18 @@
     """
     depth = compute_depth_(size);
     current_depth = 0
-    chks = list(depth * CHK_PER_INODE)
+    chks = [None] * (depth * CHK_PER_INODE) # list buffer
     read_offset = 0
-    logging.debug("Begining to calculate tree hash with depth: " + depth)
+    logging.debug("Begining to calculate tree hash with depth: "+ repr(depth))
     while True:
-        if depth is current_depth:
+        if (depth == current_depth):
             off = CHK_PER_INODE * (depth - 1)
-            logging.debug("Encoding done, reading CHK `%s' from %u\n",
-                          chks[off].query, off)
+            assert (chks[off] is not None)
+            logging.debug("Encoding done, reading CHK `"+ chks[off].query + \
+                              "' from "+ repr(off) +"\n")
             uri_chk = chks[off]
             return uri_chk
-        if current_depth is 0:
+        if (0 == current_depth):
             pt_size = min(DBLOCK_SIZE, size - read_offset);
             try:
                 pt_block = readin.read(pt_size)
@@ -148,27 +156,34 @@
                 logging.warning ("Error reading input file stream")
                 return None
         else:
-            pt_size = compute_iblock_size_(current_depth, read_offset)
-            pt_block = bytearray()
-            reduce ((lambda chk, ba:
-                         ba += chk.hash + chk.key),
-                    chks[(current_depth - 1) * CHK_PER_INODE:][:pt_size],
-                    pt_block)
+            pt_elements = compute_iblock_size_(current_depth, read_offset)
+            pt_block = ""
+            pt_block = \
+                reduce ((lambda ba, chk:
+                             ba + (chk.key + chk.query)),
+                        chks[(current_depth - 1) * 
CHK_PER_INODE:][:pt_elements],
+                        pt_block)
+            pt_size = pt_elements * (CHK_HASH_SIZE + CHK_QUERY_SIZE)
+        assert (len(pt_block) == pt_size)
+        assert (pt_size <= DBLOCK_SIZE)
         off = compute_chk_offset_ (current_depth, read_offset)
-        logging.debug ("Encoding data at offset %u and depth %u with block " \
-                           "size %u and target CHK offset %u",
-                       read_offset, current_depth, pt_size, 
-                       (current_depth * CHK_PER_INODE) + off)
-        chk = Chk()
-        chk.hash = sha512_hash (pt_block)
-        
-        if current_depth is 0:
+        logging.debug ("Encoding data at offset "+ repr(read_offset) + \
+                           " and depth "+ repr(current_depth) +" with block " \
+                           "size "+ repr(pt_size) +" and target CHK offset "+ \
+                           repr(current_depth * CHK_PER_INODE))
+        pt_hash = util.sha512_hash (pt_block)
+        pt_aes_key = util.AESKey (pt_hash)
+        pt_enc = util.aes_encrypt (pt_aes_key, pt_block)
+        pt_enc_hash = util.sha512_hash (pt_enc)
+        chk = Chk(pt_hash, pt_enc_hash)
+        chks[(current_depth * CHK_PER_INODE) + off] = chk
+        if (0 == current_depth):
             read_offset += pt_size
-            if (read_offset is size) or (read_offset % (CHK_PER_INODE * \
-                                                        DBLOCK_SIZE)) is 0:
+            if (read_offset == size) or \
+                    (0 == (read_offset % (CHK_PER_INODE * DBLOCK_SIZE))):
                 current_depth += 1
         else:
-            if (off is CHK_PER_INODE) or (read_offset is size):
-                current_depth++
+            if (CHK_PER_INODE == off) or (read_offset == size):
+                current_depth += 1
             else:
                 current_depth = 0

Modified: gnunet-update/src/gnunet_update/util.py
===================================================================
--- gnunet-update/src/gnunet_update/util.py     2012-12-26 09:08:00 UTC (rev 
25651)
+++ gnunet-update/src/gnunet_update/util.py     2012-12-26 11:59:15 UTC (rev 
25652)
@@ -94,6 +94,15 @@
     object_file.close()
     return hexdigest
 
+def sha512_hash (data):
+    """ Returns the sha512 hash of the given data. 
+    
+    data: string to hash
+    """
+    hash_obj = sha512()
+    hash_obj.update (data)
+    return hash_obj.digest()
+
 def gpg_sign_file(plain_fd, sign_fd, key_fpr, passphrase, detached=False):
     """Signs the given file with a gpg clearsign signature.
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]