Last active
June 14, 2021 11:20
-
-
Save misebox/fefb0d0bdc9a9b7c824da3903437594e to your computer and use it in GitHub Desktop.
URLsafe Base64 encoding and decoding without trailing '='
This file contains 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
from typing import Union | |
import base64 | |
def base64encode(data: Union[bytes, str]) -> bytes: | |
data = data.encode() if type(data) is str else data | |
return base64.urlsafe_b64encode(data).rstrip(b'=') | |
def base64decode(b64: Union[bytes, str]) -> bytes: | |
b64 = b64.encode() if type(b64) is str else b64 | |
return base64.urlsafe_b64decode(b64 + b'=' * (-len(b64) % 4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment