Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mrowrpurr/e44977cd4aa6fb52aad50a2d226d641e to your computer and use it in GitHub Desktop.
Save mrowrpurr/e44977cd4aa6fb52aad50a2d226d641e to your computer and use it in GitHub Desktop.
import base64
# Python port of .NET's HttpServerUtility.UrlTokenDecode
def url_token_decode(encoded_string: str) -> bytes:
# Get the padding count from the last character of the encoded string
padding_count = ord(encoded_string[-1]) - ord("0")
# Remove the last character (which indicates padding)
encoded_string = encoded_string[:-1]
# Replace - and _ to revert to standard base64
encoded_string = encoded_string.replace("-", "+").replace("_", "/")
# Add back the removed padding
encoded_string += "=" * padding_count
# Base64 decode
return base64.b64decode(encoded_string).decode("utf-8")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment