Base64 for URL variant

K

Kless

Does ruby core has any method to create a Base64 for URL variant?

---------
Using a URL-encoder on standard Base64, however, is inconvenient as it
will translate the '+' and '/' characters into special percent-encoded
hexadecimal sequences ('+' = '%2B' and '/' = '%2F'). When this is
later used with database storage or across heterogeneous systems, they
will themselves choke on the '%' character generated by URL-encoders
(because the '%' character is also used in ANSI SQL as a wildcard).

For this reason, a modified Base64 for URL variant exists, where no
padding '=' will be used, and the '+' and '/' characters of standard
Base64 are respectively replaced by '-' and '_', so that using URL
encoders/decoders is no longer necessary and has no impact on the
length of the encoded value, leaving the same encoded form intact for
use in relational databases, web forms, and object identifiers in
general.
 
R

Rob Biedenharn

CGI.escape( Base64.encode( 'foobar' ) )

is one way...

a @ http://codeforpeople.com/


Here's another:
# base64, but change + and / which are significant to URLs to -
and _,
# respectively, (which is what RFC4648 calls "base64url") and
remove
# embedded newlines (since they are recommended by MIME encoding
and not
# base64, per se)
encoded_token = [cipher_token].pack('m').tr('+/','-
_').gsub("\n",'')

the encoded_token is "safe" for a URL and the cipher_token is
retrieved with:

cipher_token = encoded_token.tr('-_','+/').unpack('m')[0]

The details of the cipher_token aren't important save that it can
contain any byte values.

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top