Created
August 8, 2023 01:51
-
-
Save mrowrpurr/e44977cd4aa6fb52aad50a2d226d641e to your computer and use it in GitHub Desktop.
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
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