Forum: Ruby Base64 for URL variant

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Kless (Guest)
on 2008-10-01 23:35
(Received via mailing list)
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
Cb9bb951f1c2d39fa5c0f334ff07ec0b?d=identicon&s=25 Ara H. (ahoward)
on 2008-10-01 23:41
(Received via mailing list)
On Oct 1, 2008, at 3:33 PM, Kless wrote:

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

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

is one way...

a @
Rob Biedenharn (Guest)
on 2008-10-02 00:10
(Received via mailing list)
On Oct 1, 2008, at 5:40 PM, ara.t.howard wrote:

> a @
> --
> we can deny everything, except that we have the possibility of being
> better. simply reflect on that.
> h.h. the 14th dalai lama

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

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

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

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


Rob Biedenharn
This topic is locked and can not be replied to.