Created
January 25, 2024 12:58
-
-
Save tommilligan/121b2fda8078370b5720fd11743a0e84 to your computer and use it in GitHub Desktop.
Pad and unpad base64 strings based on length
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| _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