Skip to content

Instantly share code, notes, and snippets.

@smowtion
Forked from perrygeo/base64_padding.md
Created March 7, 2022 16:14
Show Gist options
  • Save smowtion/ee2b4900181fa105653114ce26224392 to your computer and use it in GitHub Desktop.
Save smowtion/ee2b4900181fa105653114ce26224392 to your computer and use it in GitHub Desktop.
Avoiding TypeError: Incorrect padding with Python's base64 encoding

Avoiding padding errors with Python's base64 encoding

>>> import base64
>>> data = '{"u": "test"}'
>>> code = base64.b64encode(data)
>>> code
'eyJ1IjogInRlc3QifQ=='

Note the trailing == to make len a multiple of 4. This decodes properly

>>> len(code)
20
>>> base64.b64decode(code)
'{"u": "test"}'
>>> base64.b64decode(code) == data
True

without the == padding (this is how many things are encoded for e.g. access tokens)

>>> base64.b64decode(code[0:18]) == data
...
TypeError: Incorrect padding 

However, you can add back the padding

>>> base64.b64decode(code + "==") == data
True

Or add an arbitrary amount of padding (it will ignore extraneous padding)

>>> base64.b64decode(code + "========") == data
True

This last property of python's base64 decoding ensures that the following code adding 3 padding = will never succumb to the TypeError and will always produce the same result.

>>> base64.b64decode(code + "===") == data
True

It's clumsy but effective method to deal with strings from different implementations of base64 encoders

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment