Skip to content

Instantly share code, notes, and snippets.

@tommilligan
Created January 25, 2024 12:58
Show Gist options
  • Select an option

  • Save tommilligan/121b2fda8078370b5720fd11743a0e84 to your computer and use it in GitHub Desktop.

Select an option

Save tommilligan/121b2fda8078370b5720fd11743a0e84 to your computer and use it in GitHub Desktop.
Pad and unpad base64 strings based on length
_BASE64_PADDING = "="
def strip_base64_padding(string: str) -> str:
"""Strip padding of equals sign from base64. Required for use in urls."""
return string.rstrip(_BASE64_PADDING)
def unstrip_base64_padding(string: str) -> str:
"""Re-pad a base64 string with the right amount of padding."""
padding_length = 4 - (len(string) % 4)
if padding_length == 4:
return string
return string + (_BASE64_PADDING * padding_length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment