oldhomemovie
oldhomemovie

Reputation: 15129

Rails: base64 and character escaping problem

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.

Upvotes: 1

Views: 2003

Answers (2)

nicholasklick
nicholasklick

Reputation: 1212

If you need to escape html you can also do:

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

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

Upvotes: 0

Marc-Andr&#233; Lafortune
Marc-Andr&#233; Lafortune

Reputation: 79612

Use CGI.escape instead :-)

require 'cgi'
puts CGI.escape('/') # => "%2F"

Upvotes: 3

Related Questions