Marten4n6 / TinyTor

A tiny Tor client implementation (in pure python).
GNU General Public License v3.0
132 stars 28 forks source link

base64 missing padding chars #2

Closed lontivero closed 6 years ago

lontivero commented 6 years ago

I've tried to make a PR but Github is completely broken today: https://twitter.com/lontivero/status/1054160998259658752

diff --git a/tinytor.py b/tinytor.py
index 40e11e0..e7b3ecc 100644
--- a/tinytor.py
+++ b/tinytor.py
@@ -186,16 +186,11 @@ class Consensus:
                 tor_port = int(split_line[7])
                 dir_port = int(split_line[8])

-                while True:
-                    try:
-                        # The fingerprint here is base64 encoded bytes.
-                        # The descriptor URL uses the base16 encoded value of these bytes.
-                        # Documentation for this was hard to find...
-                        identity = b16encode(b64decode(identity.encode())).decode()
-                        break
-                    except binascii.Error:
-                        # Incorrect base64 padding.
-                        identity = identity + "="
+                # The fingerprint here is base64 encoded bytes.
+                # The descriptor URL uses the base16 encoded value of these bytes.
+                # Documentation for this was hard to find...
+                identity += '=' * (-len(identity) % 4)
+                identity = b16encode(b64decode(identity.encode())).decode()

                 onion_router = OnionRouter(nickname, ip, dir_port, tor_port, identity)
             elif line.startswith("s "):

patch.txt

Marten4n6 commented 6 years ago

I've accepted your pull request, thanks.