r183 - /debtorrent/trunk/hippy.py

camrdale-guest at users.alioth.debian.org camrdale-guest at users.alioth.debian.org
Tue Jul 17 08:22:50 UTC 2007


Author: camrdale-guest
Date: Tue Jul 17 08:22:50 2007
New Revision: 183

URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=183
Log:
Includes SHA1 of the entire file (from aj).

Modified:
    debtorrent/trunk/hippy.py

Modified: debtorrent/trunk/hippy.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/hippy.py?rev=183&op=diff
==============================================================================
--- debtorrent/trunk/hippy.py (original)
+++ debtorrent/trunk/hippy.py Tue Jul 17 08:22:50 2007
@@ -7,11 +7,13 @@
 
 def hash(file, piecesize):
         h = []
+	fullh = sha.new()
         while 1:
                 x = file.read(piecesize)
                 if x == "": break
                 h.append((sha.new(x).hexdigest(), len(x)))
-        return h
+		fullh.update(x)
+        return (fullh.hexdigest(), h)
 
 piecesize = 512*1024
 chunksize = 16*1024
@@ -37,13 +39,16 @@
 
 def str2hash(s):
     r = []
+    if s == "": return None, []
+
+    fh,s = binascii.b2a_hex(s[:20]), s[20:]
     while len(s) > 0:
         (l,h,s) = s[:4], s[4:24], s[24:]
 	r.append( (binascii.b2a_hex(h), long(binascii.b2a_hex(l), 16)) )
-    return r
+    return fh,r
 
-def hash2str(hs):
-    s = ""
+def hash2str(fh, hs):
+    s = binascii.a2b_hex(fh)
     for (h, l) in hs:
 	s += binascii.a2b_hex("%08x" % l) + binascii.a2b_hex(h)
     return s
@@ -52,7 +57,7 @@
     filename = filename.rstrip()
     fnkey = filename + ":pc"
     if cache.has_key(fnkey):
-    	result = str2hash(cache[fnkey])
+    	sha1, result = str2hash(cache[fnkey])
     else:
     	size = os.stat(filename).st_size
     	if size <= piecesize:
@@ -61,13 +66,14 @@
 	else:
         	ps = optimalpiecesize(size)
         	file = open(filename)
-        	result = hash(file, ps)
-		values = hash2str(result)
+        	sha1, result = hash(file, ps)
+		values = hash2str(sha1, result)
         	file.close()
 	cache[fnkey] = values
 
     if result:
     	print "Filename: %s" % (filename)
+	print "SHA1: %s" % (sha1)
     	print "SHA1-Pieces:"
     	for x in result:
             print " %s %d" % x




More information about the Debtorrent-commits mailing list