views:

165

answers:

2

In my app I need to encode a string via base64, escape it's possible special characters and put it into a URL.

I do the following:

string = "[email protected]"

enc = OpenSSL::Cipher::Cipher.new('DES-EDE3-CBC')
enc.encrypt('dummy_salt')
encoded = URI.escape(Base64.encode64(enc.update(string) << enc.final))

The problem is, that somehow URI.escape do not escape '/' character. That's completely unacceptable if the encoded string is intended to be used as a URL parameter.

How come URI.escape ignores to escape '/'? Should I user any other .escape then one, which comes from URI? Or should I even use other encoding method (don't think so)?

Any suggestions as to the code are welcome too.

+2  A: 

Use CGI.escape instead :-)

require 'cgi'
puts CGI.escape('/') # => "%2F"
Marc-André Lafortune
brilliant, thanks
gmile
A: 

If you need to escape html you can also do:

CGI::escapeHTML('Usage: foo "bar" <baz>')

"Usage: foo &quot;bar&quot; &lt;baz&gt;"
nicholasklick