[tor-commits] [bridgedb/develop] Add a bridgedb.bridges.Bridge.__str__ method.

isis at torproject.org isis at torproject.org
Thu Feb 19 02:21:13 UTC 2015


commit fe405ac660ad1258fbc31d32f80aae550c31099b
Author: Isis Lovecruft <isis at torproject.org>
Date:   Sat Dec 6 01:10:10 2014 +0000

    Add a bridgedb.bridges.Bridge.__str__ method.
---
 lib/bridgedb/bridges.py |   31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/lib/bridgedb/bridges.py b/lib/bridgedb/bridges.py
index 100dd12..cb81dff 100644
--- a/lib/bridgedb/bridges.py
+++ b/lib/bridgedb/bridges.py
@@ -11,6 +11,7 @@
 
 from __future__ import print_function
 
+import hashlib
 import ipaddr
 import logging
 import os
@@ -465,6 +466,36 @@ class Bridge(object):
                                       idDigest=id_digest,
                                       orAddresses=or_addresses)
 
+    def __str__(self):
+        """Return a pretty string representation that identifies this Bridge.
+
+        .. warning:: With safelogging disabled, the returned string contains
+            the bridge's fingerprint, which should be handled with care.
+
+        If safelogging is enabled, the returned string will have the SHA-1
+        hash of the bridge's fingerprint (a.k.a. a hashed fingerprint).
+
+        Hashed fingerprints will be prefixed with ``'$$'``, and the real
+        fingerprints are prefixed with ``'$'``.
+
+        :rtype: str
+        :returns: A string in the form:
+            :data:`nickname```.$``:data:`fingerprint`.
+        """
+        nickname = self.nickname if self.nickname else 'Unnamed'
+        separator = '.$'
+        fingerprint = self.fingerprint
+
+        if safelog.safe_logging:
+            separator = '.$$'
+            if fingerprint:
+                fingerprint = hashlib.sha1(fingerprint).hexdigest()
+
+        if not fingerprint:
+            fingerprint = '0' * 40
+
+        return nickname + separator + fingerprint
+
     def _backwardsCompatible(self, nickname=None, address=None, orPort=None,
                              fingerprint=None, idDigest=None,
                              orAddresses=None):





More information about the tor-commits mailing list