[mirrorbrain-commits] r8074 - /trunk/mb/mb/hashes.py

From: <poeml_at_mirrorbrain.org>
Date: Thu, 05 Aug 2010 02:03:28 -0000
Author: poeml
Date: Thu Aug  5 04:03:28 2010
New Revision: 8074

URL: http://svn.mirrorbrain.org/viewvc/mirrorbrain?rev=8074&view=rev
Log:
mb makehashes:
- additionally create a "bittorrent information hash" and store it in the
  database (needed for magnet links, see issue #56)

Modified:
    trunk/mb/mb/hashes.py

Modified: trunk/mb/mb/hashes.py
URL: http://svn.mirrorbrain.org/viewvc/mirrorbrain/trunk/mb/mb/hashes.py?rev=8074&r1=8073&r2=8074&view=diff
==============================================================================
--- trunk/mb/mb/hashes.py (original)
+++ trunk/mb/mb/hashes.py Thu Aug  5 04:03:28 2010
_at_@ -22,6 +22,7 @@
     sha256 = None
 
 PIECESIZE = 262144
+SHA1_DIGESTSIZE = 20
 
 # must be a multiple of 2048 and 4096 for zsync checksumming
 assert PIECESIZE % 4096 == 0
_at_@ -150,11 +151,12 @@
 
             c.execute("""INSERT INTO hash (file_id, mtime, size, md5, 
                                            sha1, sha256, sha1piecesize, 
-                                           sha1pieces, pgp, zblocksize,
+                                           sha1pieces, btih, pgp, zblocksize,
                                            zhashlens, zsums) 
                          VALUES (%s, %s, %s, 
                                  decode(%s, 'hex'), decode(%s, 'hex'), 
                                  decode(%s, 'hex'), %s, decode(%s, 'hex'),
+                                 decode(%s, 'hex'),
                                  %s, %s, %s, decode(%s, 'hex'))""",
                       [file_id, int(self.mtime), self.size,
                        self.hb.md5hex or '',
_at_@ -162,6 +164,7 @@
                        self.hb.sha256hex or '',
                        PIECESIZE,
                        ''.join(self.hb.pieceshex),
+                       self.hb.btihhex or '',
                        self.hb.pgp or '',
                        self.hb.zblocksize,
                        self.hb.get_zparams(),
_at_@ -190,6 +193,7 @@
                                          sha256 = decode(%s, 'hex'), 
                                          sha1piecesize = %s,
                                          sha1pieces = decode(%s, 'hex'), 
+                                         btih = decode(%s, 'hex'),
                                          pgp = %s,
                                          zblocksize = %s,
                                          zhashlens = %s,
_at_@ -198,6 +202,7 @@
                       [int(self.mtime), self.size,
                        self.hb.md5hex or '', self.hb.sha1hex or '', self.hb.sha256hex or '',
                        PIECESIZE, ''.join(self.hb.pieceshex),
+                       self.hb.btihhex or '',
                        self.hb.pgp or '', 
                        self.hb.zblocksize,
                        self.hb.get_zparams(),
_at_@ -233,6 +238,8 @@
         self.npieces = 0
         self.pieces = []
         self.pieceshex = []
+        self.btih = None
+        self.btihhex = None
 
         self.do_zsync = False
         self.zsums = []
_at_@ -294,6 +301,8 @@
         if sha256:
             self.sha256 = s256.digest()
             self.sha256hex = s256.hexdigest()
+
+        self.calc_btih()
 
         # if present, grab PGP signature
         if os.path.exists(self.src + '.asc'):
_at_@ -315,6 +324,7 @@
         r.append('sha1 %s' % self.sha1hex)
         if sha256:
             r.append('sha256 %s' % self.sha256hex)
+        r.append('btih %s' % self.btihhex)
         return '\n'.join(r)
 
 
_at_@ -428,3 +438,18 @@
                 self.zsums.append( c[0:self.zchecksum_len] )  # save only some leading bytes
 
 
+    def calc_btih(self):
+        """ calculate a bittorrent information hash (btih) """
+
+        buf = ['d', 
+                 '6:length', 'i', str(self.h.size), 'e',
+                 '4:name', str(len(self.basename)), ':', self.basename, 
+                 '12:piece length', 'i', str(PIECESIZE), 'e',
+                 '6:pieces', str(len(self.pieces) * SHA1_DIGESTSIZE), ':', ''.join(self.pieces),
+               'e']
+
+        h = sha1.sha1()
+        h.update(''.join(buf))
+        self.btih = h.digest()
+        self.btihhex = h.hexdigest()
+




_______________________________________________
mirrorbrain-commits mailing list
Archive: http://mirrorbrain.org/archive/mirrorbrain-commits/

Note: To remove yourself from this list, send a mail with the content
 	unsubscribe
to the address mirrorbrain-commits-request_at_mirrorbrain.org
Received on Thu Aug 05 2010 - 02:03:29 GMT

This archive was generated by hypermail 2.3.0 : Mon Feb 20 2012 - 23:47:04 GMT