Why does Python's base64.b64decode() ignore gibberish at end of string?
Question: I have a long token that I'm decoding using python's `base64.b64decode()`
method.

It works. But as you can see below, it returns the same result even if I
insert gibberish characters at the end. Why? Shouldn't these two strings
produce two different decoded results?

    
    
    >>> import base64
    >>> token = "Ti6VXtqWYb8WVuR6m/bnDKyVqS96pvRGH9SqTsC7w1E4ZlcjCK8SDQFWRa2b0q96pAflgZTmao+CeEk9cJFVUq0MgBCBoPMUEdTLwT7AhyAa1xOQf8b9C63+DH3v2L+PqJMPSTPfWRXL5WeOPR+gFJBrAm/658phg6vzhBMNS6wgyiiLqfWUOpWyAlcMRrKu5Yq7mXaloxxFQm6HEVcvrjDVGSdsCHRB0Osby8PttEel5oqFkYq85LfNobE9VaR6Onzowru1lHnTdfEqUT5qabXaw9j9rapT4+in2N1WQt1t+XzBn1xxGLT903FOZQxkf2X7R9sGrhLXzSnBAW5q18T8ZJBsxsq3OryCgKfPEJ3x+uj0LCnoogX/gucVcZDp19HIdvelOQsD5de85U800LCDQFKatd/+VBhh4oRrnefD+6l4WRzjg1h5J2ZNgjUhCtIu6r63zFq5ef7nG60JxdTYPOT1njGfEUNAuNuBW97i98ZfhmiPOZMaINPoEFHJQRG1nMwAYCwcytn053n+7D5Dz6MZxrWwAX3/VS9fT6SduFVQ6X4HJA/+FIH8epcqAkU6M6UVm7sfQwHV/vflVAkGNQFevNwA2+u6erInPTWqL9usz4IU47ekp68xk1BBAYEqE0AKeXaZZVpYJ8CJmbAcdxvMD9+Pchi9lk6ZomzxxLKWEPGcPjFobM8bRDEVbmfP+vYfWwovy/tOo9tqkqc0sAvS5RGp9Q0SBAfBQ9c8TXuwqrDBc0OPG5TTEQQ42Cd9Ky9K2ZHldQkXOc/H0vIWBo2m5aJABvVWambd0oEzGmQHrNYzQxNSKgWSLoh7w8HrUzn9skJQGzU/igt6EOdp617ToBD5G936ByF7Rft+FGKB3jiFeEvke0Fbh3wrsr0xqP9JxL/tr8P2x29hRQauigY2MYwrt0nilET/x88="
    >>> base64.b64decode("%sXXXXXXBlahBlahBlah" % (token)) == base64.b64decode(token)
    True
    

If I put the gibberish characters at the beginning, it fails:

    
    
    >>> base64.b64decode("%sXXXXXXBlahBlahBlah" % (token)) == base64.b64decode(token)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/base64.py", line 76, in b64decode
        raise TypeError(msg)
    TypeError: Incorrect padding
    


Answer: [The CPython
implementation](https://hg.python.org/cpython/file/2.7/Modules/binascii.c#l403)
stop interpreting if it see a pad (`=`).

    
    
        if (this_ch == BASE64_PAD) {
            if ( (quad_pos < 2) ||
                 ((quad_pos == 2) &&
                  (binascii_find_valid(ascii_data, ascii_len, 1)
                   != BASE64_PAD)) )
            {
                continue;
            }
            else {
                /* A pad sequence means no more input.
                ** We've already interpreted the data
                ** from the quad at this point.
                */
                leftbits = 0;
                break;
            }
        }
    

An experiment with base64-encoded string without padding:

    
    
    >>> base64.decodestring('YWJj')  # without a padding
    'abc'
    >>> base64.decodestring('YWJj' + 'XXX')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python2.7/base64.py", line 328, in decodestring
        return binascii.a2b_base64(s)
    binascii.Error: Incorrect padding
    
    >>> base64.decodestring('YWI=')  # with a padding
    'ab'
    >>> base64.decodestring('YWI=XXX')
    'ab'
    

