Skip to content

Instantly share code, notes, and snippets.

@misebox
Last active June 14, 2021 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misebox/fefb0d0bdc9a9b7c824da3903437594e to your computer and use it in GitHub Desktop.
Save misebox/fefb0d0bdc9a9b7c824da3903437594e to your computer and use it in GitHub Desktop.
URLsafe Base64 encoding and decoding without trailing '='
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