self.done = True
self.result = None
- def _norm_hash(self, hashString, bits=None, bytes=None):
- if bits is not None:
- bytes = (bits - 1) // 8 + 1
- else:
- if bytes is None:
- raise HashError, "you must specify one of bits or bytes"
- if len(hashString) < bytes:
- hashString = hashString + '\000'*(bytes - len(hashString))
- elif len(hashString) > bytes:
- hashString = hashString[:bytes]
- return hashString
-
#### Methods for returning the expected hash
def expected(self):
"""Get the expected hash."""
self.expHex = b2a_hex(self.expHash)
return self.expHex
- def normexpected(self, bits=None, bytes=None):
- """Normalize the binary hash for the given length.
-
- You must specify one of bits or bytes.
- """
- if self.expNormHash is None and self.expHash is not None:
- self.expNormHash = self._norm_hash(self.expHash, bits, bytes)
- return self.expNormHash
-
#### Methods for hashing data
def new(self, force = False):
"""Generate a new hashing object suitable for hashing a file.
self.fileHex = b2a_hex(self.digest())
return self.fileHex
- def norm(self, bits=None, bytes=None):
- """Normalize the binary hash for the given length.
-
- You must specify one of bits or bytes.
- """
- if self.fileNormHash is None:
- self.fileNormHash = self._norm_hash(self.digest(), bits, bytes)
- return self.fileNormHash
-
def verify(self):
"""Verify that the added file data hash matches the expected hash."""
if self.result is None and self.fileHash is not None and self.expHash is not None:
if sys.version_info < (2, 4):
skip = "skippingme"
- def test_normalize(self):
- h = HashObject()
- h.set(h.ORDER[0], b2a_hex('12345678901234567890'), '0')
- self.failUnless(h.normexpected(bits = 160) == '12345678901234567890')
- h = HashObject()
- h.set(h.ORDER[0], b2a_hex('12345678901234567'), '0')
- self.failUnless(h.normexpected(bits = 160) == '12345678901234567\000\000\000')
- h = HashObject()
- h.set(h.ORDER[0], b2a_hex('1234567890123456789012345'), '0')
- self.failUnless(h.normexpected(bytes = 20) == '12345678901234567890')
- h = HashObject()
- h.set(h.ORDER[0], b2a_hex('1234567890123456789'), '0')
- self.failUnless(h.normexpected(bytes = 20) == '1234567890123456789\000')
- h = HashObject()
- h.set(h.ORDER[0], b2a_hex('123456789012345678901'), '0')
- self.failUnless(h.normexpected(bits = 160) == '12345678901234567890')
-
def test_failure(self):
h = HashObject()
h.set(h.ORDER[0], b2a_hex('12345678901234567890'), '0')
- self.failUnlessRaises(HashError, h.normexpected)
self.failUnlessRaises(HashError, h.digest)
self.failUnlessRaises(HashError, h.hexdigest)
self.failUnlessRaises(HashError, h.update, 'gfgf')
def lookupHash(self, hash, path, d):
log.msg('Looking up hash in DHT for file: %s' % path)
- key = hash.normexpected(bits = config.getint(config.get('DEFAULT', 'DHT'), 'HASH_LENGTH'))
+ key = hash.expected()
lookupDefer = self.dht.getValue(key)
lookupDefer.addCallback(self.lookupHash_done, hash, path, d)
def store(self, hash):
"""Add a file to the DHT."""
- key = hash.norm(bits = config.getint(config.get('DEFAULT', 'DHT'), 'HASH_LENGTH'))
+ key = hash.digest()
value = {'c': self.my_contact}
pieces = hash.pieceDigests()
if len(pieces) <= 1:
self.joined = False
self.khashmir.shutdown()
+ def _normKey(self, key, bits=None, bytes=None):
+ bits = self.config["HASH_LENGTH"]
+ if bits is not None:
+ bytes = (bits - 1) // 8 + 1
+ else:
+ if bytes is None:
+ raise DHTError, "you must specify one of bits or bytes for normalization"
+ if len(key) < bytes:
+ key = key + '\000'*(bytes - len(key))
+ elif len(key) > bytes:
+ key = key[:bytes]
+ return key
+
def getValue(self, key):
"""See L{apt_dht.interfaces.IDHT}."""
if self.config is None:
raise DHTError, "configuration not loaded"
if not self.joined:
raise DHTError, "have not joined a network yet"
+
+ key = self._normKey(key)
d = defer.Deferred()
if key not in self.retrieving:
if not self.joined:
raise DHTError, "have not joined a network yet"
+ key = self._normKey(key)
bvalue = bencode(value)
if key in self.storing and bvalue in self.storing[key]:
d.addCallback(self.lastDefer.callback)
return self.lastDefer
+ def test_normKey(self):
+ h = self.a._normKey('12345678901234567890')
+ self.failUnless(h == '12345678901234567890')
+ h = self.a._normKey('12345678901234567')
+ self.failUnless(h == '12345678901234567\000\000\000')
+ h = self.a._normKey('1234567890123456789012345')
+ self.failUnless(h == '12345678901234567890')
+ h = self.a._normKey('1234567890123456789')
+ self.failUnless(h == '1234567890123456789\000')
+ h = self.a._normKey('123456789012345678901')
+ self.failUnless(h == '12345678901234567890')
+
def value_stored(self, result, value):
self.stored -= 1
if self.stored == 0: