commit bc8b04929fccdcf1f7d7f6c162a677cae4ccfa70 Author: Isis Lovecruft isis@torproject.org Date: Fri Nov 15 15:39:20 2013 +0000
Add parse.padBase64() function for adding stripped padding '==' back in. --- lib/bridgedb/parse/__init__.py | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+)
diff --git a/lib/bridgedb/parse/__init__.py b/lib/bridgedb/parse/__init__.py index e69de29..1fdbd8a 100644 --- a/lib/bridgedb/parse/__init__.py +++ b/lib/bridgedb/parse/__init__.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +# +# This file is part of BridgeDB, a Tor bridge distribution system. +# +# :authors: Isis Lovecruft 0xA3ADB67A2CDB8B35 isis@torproject.org +# please also see AUTHORS file +# :copyright: (c) 2013 Isis Lovecruft +# (c) 2007-2013, The Tor Project, Inc. +# (c) 2007-2013, all entities within the AUTHORS file +# :license: 3-clause BSD, see included LICENSE for information + +"""Modules for parsing data. + +** Package Overview: ** + +.. + parse + ||_ parse.addr + | |_ isIPAddress - Check if an arbitrary string is an IP address. + | |_ isIPv4 - Check if an arbitrary string is an IPv4 address. + | |_ isIPv6 - Check if an arbitrary string is an IPv6 address. + | _ isValidIP - Check that an IP address is valid. + | + |__ :mod:`bridgedbparse.headers` + |__ :mod:`bridgedb.parse.options` + __ :mod:`bridgedb.parse.versions` +""" + + +def padBase64(b64string): + """Re-add any stripped equals sign character padding to a b64 string. + + :param string b64string: A base64-encoded string which might have had its + trailing equals sign padding removed. + """ + try: + b64string = b64string.strip() + except AttributeError: + logging.error("Cannot pad base64 string %r: not a string." % b64string) + else: + addchars = 0 + remainder = len(b64string) % 4 + if 2 <= remainder <= 3: + addchars = 4 - remainder + else: + raise ValueError("Invalid base64-encoded string: %r" % b64string) + b64string += '=' * addchars + finally: + return b64string
tor-commits@lists.torproject.org