[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [bridgedb/develop] Add Bridge.identity property and replace all usage of getID().



commit 5fec5eb3e5f8901a08a741ab57cf48b9126e41ee
Author: Isis Lovecruft <isis@xxxxxxxxxxxxxx>
Date:   Sat Apr 18 00:48:26 2015 +0000

    Add Bridge.identity property and replace all usage of getID().
---
 lib/bridgedb/Bridges.py |   25 ++++++++++---------------
 lib/bridgedb/Filters.py |    2 +-
 lib/bridgedb/bridges.py |   33 +++++++++++++++++++++++++++++++--
 3 files changed, 42 insertions(+), 18 deletions(-)

diff --git a/lib/bridgedb/Bridges.py b/lib/bridgedb/Bridges.py
index b8fdd2e..5742794 100644
--- a/lib/bridgedb/Bridges.py
+++ b/lib/bridgedb/Bridges.py
@@ -25,8 +25,6 @@ import bridgedb.Bucket
 from bridgedb.bridges import Bridge
 from bridgedb.crypto import getHMACFunc
 from bridgedb.parse import addr
-from bridgedb.parse.fingerprint import toHex
-from bridgedb.parse.fingerprint import fromHex
 from bridgedb.parse.fingerprint import isValidFingerprint
 from bridgedb.safelog import logSafely
 
@@ -221,13 +219,12 @@ class BridgeRing(BridgeHolder):
                 if val == 'stable' and bridge.flags.stable:
                     subring.insert(bridge)
 
-        ident = bridge.getID()
-        pos = self.hmac(ident)
-        if not self.bridges.has_key(pos):
+        pos = self.hmac(bridge.identity)
+        if not pos in self.bridges:
             self.sortedKeys.append(pos)
             self.isSorted = False
         self.bridges[pos] = bridge
-        self.bridgesByID[ident] = bridge
+        self.bridgesByID[bridge.identity] = bridge
         logging.debug("Adding %s to %s" % (bridge.address, self.name))
 
     def _sort(self):
@@ -330,11 +327,11 @@ class BridgeRing(BridgeHolder):
         logging.info("Dumping bridge assignments for %s..." % self.name)
         for b in self.bridges.itervalues():
             desc = [ description ]
-            ident = b.getID()
             for tp,val,_,subring in self.subrings:
-                if subring.getBridgeByID(ident):
+                if subring.getBridgeByID(b.identity):
                     desc.append("%s=%s"%(tp,val))
-            f.write("%s %s\n"%( toHex(ident), " ".join(desc).strip()))
+            f.write("%s %s\n" % (b.fingerprint, " ".join(desc).strip()))
+
 
 class FixedBridgeSplitter(BridgeHolder):
     """A bridgeholder that splits bridges up based on an hmac and assigns
@@ -348,7 +345,7 @@ class FixedBridgeSplitter(BridgeHolder):
 
     def insert(self, bridge):
         # Grab the first 4 bytes
-        digest = self.hmac(bridge.getID())
+        digest = self.hmac(bridge.identity)
         pos = long( digest[:8], 16 )
         which = pos % len(self.rings)
         self.rings[which].insert(bridge)
@@ -467,11 +464,9 @@ class BridgeSplitter(BridgeHolder):
         if not bridge.flags.running:
             return
 
-        bridgeID = bridge.fingerprint
-
         # Determine which ring to put this bridge in if we haven't seen it
         # before.
-        pos = self.hmac(bridgeID)
+        pos = self.hmac(bridge.identity)
         n = int(pos[:8], 16) % self.totalP
         pos = bisect.bisect_right(self.pValues, n) - 1
         assert 0 <= pos < len(self.rings)
@@ -668,7 +663,7 @@ class FilteredBridgeSplitter(BridgeHolder):
                 if g(b):
                     # ghetto. get subring flags, ports
                     for tp,val,_,subring in r.subrings:
-                        if subring.getBridgeByID(b.getID()):
+                        if subring.getBridgeByID(b.identity):
                             desc.append("%s=%s"%(tp,val))
                     try:
                         desc.extend(g.description.split())
@@ -693,4 +688,4 @@ class FilteredBridgeSplitter(BridgeHolder):
             # add to assignments
             desc = "%s %s" % (description.strip(),
                     " ".join([v for k,v in grouped.items()]).strip())
-            f.write("%s %s\n"%( toHex(b.getID()), desc))
+            f.write("%s %s\n" % (b.fingerprint, desc))
diff --git a/lib/bridgedb/Filters.py b/lib/bridgedb/Filters.py
index 41df297..fb0197b 100644
--- a/lib/bridgedb/Filters.py
+++ b/lib/bridgedb/Filters.py
@@ -17,7 +17,7 @@ def filterAssignBridgesToRing(hmac, numRings, assignedRing):
         return funcs[ruleset]
     except KeyError:
         def _assignBridgesToRing(bridge):
-            digest = hmac(bridge.getID())
+            digest = hmac(bridge.identity)
             pos = long( digest[:8], 16 )
             which = pos % numRings + 1
 
diff --git a/lib/bridgedb/bridges.py b/lib/bridgedb/bridges.py
index 541e735..c8c7a30 100644
--- a/lib/bridgedb/bridges.py
+++ b/lib/bridgedb/bridges.py
@@ -178,6 +178,36 @@ class BridgeAddressBase(object):
         self._fingerprint = None
 
     @property
+    def identity(self):
+        """Get this Bridge's identity digest.
+
+        :rtype: bytes
+        :returns: The binary-encoded SHA-1 hash digest of the public half of
+            this Bridge's identity key, if available; otherwise, returns
+            ``None``.
+        """
+        if self.fingerprint:
+            return fromHex(self.fingerprint)
+
+    @identity.setter
+    def identity(self, value):
+        """Set this Bridge's identity digest to **value**.
+
+        .. info: The purported identity digest will be checked for
+            specification conformity with
+            :func:`~bridgedb.parse.fingerprint.isValidFingerprint`.
+
+        :param str value: The binary-encoded SHA-1 hash digest of the public
+            half of this Bridge's identity key.
+        """
+        self.fingerprint = toHex(value)
+
+    @identity.deleter
+    def identity(self):
+        """Reset this Bridge's identity digest."""
+        del(self.fingerprint)
+
+    @property
     def address(self):
         """Get this bridge's address.
 
@@ -672,8 +702,7 @@ class BridgeBackwardsCompatibility(BridgeBase):
         This method is provided for backwards compatibility and should not
         be relied upon.
         """
-        if self.fingerprint:
-            return fromHex(self.fingerprint)
+        return self.identity
 
     def setDescriptorDigest(self, digest):
         """Set this ``Bridge``'s server-descriptor digest.



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits