Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat character replacements should be performed to make base 64 encoding URL safe?
    primarykey
    data
    text
    <p>In looking at URL safe base 64 encoding, I've found it to be a very non-standard thing. Despite the copious number of built in functions that PHP has, there isn't one for URL safe base 64 encoding. On the manual page for <a href="http://us.php.net/base64_encode" rel="nofollow noreferrer"><code>base64_encode()</code></a>, most of the comments suggest using that function, wrapped with <code>strtr()</code>:</p> <pre><code>function base64_url_encode($input) { return strtr(base64_encode($input), '+/=', '-_,'); } </code></pre> <p>The only Perl module I could find in this area is <a href="http://search.cpan.org/~kazuho/MIME-Base64-URLSafe-0.01/lib/MIME/Base64/URLSafe.pm" rel="nofollow noreferrer">MIME::Base64::URLSafe</a> (<a href="http://cpansearch.perl.org/src/KAZUHO/MIME-Base64-URLSafe-0.01/lib/MIME/Base64/URLSafe.pm" rel="nofollow noreferrer">source</a>), which performs the following replacement internally:</p> <pre><code>sub encode ($) { my $data = encode_base64($_[0], ''); $data =~ tr|+/=|\-_|d; return $data; } </code></pre> <p>Unlike the PHP function above, this Perl version drops the '=' (equals) character entirely, rather than replacing it with ',' (comma) as PHP does. Equals is a padding character, so the Perl module replaces them as needed upon decode, but this difference makes the two implementations incompatible.</p> <p>Finally, the Python function <a href="http://docs.python.org/library/base64.html#base64.urlsafe_b64encode" rel="nofollow noreferrer">urlsafe_b64encode(s)</a> keeps the '=' padding around, prompting someone to put up <a href="http://snipplr.com/view/15110/urlsafe-base64-functions/" rel="nofollow noreferrer">this function</a> to remove the padding which shows prominently in Google results for <a href="http://www.google.com/search?hl=en&amp;q=python%20base64%20url%20safe&amp;btnG=Google+Search" rel="nofollow noreferrer">'python base64 url safe'</a>:</p> <pre><code>from base64 import urlsafe_b64encode, urlsafe_b64decode def uri_b64encode(s): return urlsafe_b64encode(s).strip('=') def uri_b64decode(s): return urlsafe_b64decode(s + '=' * (4 - len(s) % 4)) </code></pre> <p>The desire here is to have a string that can be included in a URL without further encoding, hence the ditching or translation of the characters '+', '/', and '='. Since there isn't a defined standard, <strong>what is the right way?</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload