def Base64WSDecode(s): """ Return decoded version of given Base64 string. Ignore whitespace. Uses URL-safe alphabet: - replaces +, _ replaces /. Will convert s of type unicode to string type first. @param s: Base64 string to decode @type s: string @return: original string that was encoded as Base64 @rtype: string @raise Base64DecodingError: If length of string (ignoring whitespace) is one more than a multiple of four. """ s = ''.join(s.splitlines()) s = str(s.replace(" ", "")) # kill whitespace, make string (not unicode) d = len(s) % 4 if d == 1: raise errors.Base64DecodingError() elif d == 2: s += "==" elif d == 3: s += "=" try: return base64.urlsafe_b64decode(s) except TypeError: # Decoding raises TypeError if s contains invalid characters. raise errors.Base64DecodingError()
def Decode(s): """ Return decoded version of given Base64 string. Ignore whitespace. @param s: Base64 string to decode @type s: string @return: original string that was encoded as Base64 @rtype: string @raise Base64DecodingError: If length of string (ignoring whitespace) is one more than a multiple of four. """ s = str(s.replace(" ", "")) # kill whitespace, make string (not unicode) d = len(s) % 4 if d == 1: raise errors.Base64DecodingError() elif d == 2: s += "==" elif d == 3: s += "=" return base64.b64decode(s)