Created
February 14, 2016 08:48
-
-
Save hungtatai/dbfbb65b10e745e9d72e to your computer and use it in GitHub Desktop.
Passing base64 encoded strings in URL
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
function base64_url_encode($input) { | |
//return strtr(base64_encode($input), '+/=', '-_,'); | |
return strtr(base64_encode($input), '+/=', '-_~'); | |
} | |
function base64_url_decode($input) { | |
//return base64_decode(strtr($input, '-_,', '+/=')); | |
return base64_decode(strtr($input, '-_~', '+/=')); | |
} | |
import base64 | |
def urlsafe_b64encode(s): | |
return base64.b64encode(s.encode('utf-8')).decode('utf-8').replace("+", "-").replace("/", "_").replace("=", "~") | |
def urlsafe_b64decode(s): | |
return base64.b64decode(s.replace("-", "+").replace("_", "/").replace("~", "=").encode('utf-8')).decode('utf-8') | |
# @RandalSchwartz tilde is URL-safe. From RFC3986: unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" – kralyk Sep 30 '15 at 10:53 | |
# | |
# http://stackoverflow.com/questions/1374753/passing-base64-encoded-strings-in-url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment