Simple cipher to encode url

I want to encode a url ( http://www.someurl.com ) so I can add it as a parameter to the GET call:

domain.com/encoded_url

Once received on the other end, I should be able to decode the URL, however security is not an issue. I tend to stick with simple encryption (should be able to handle special characters like ":" and "?"), Since more advanced ciphers create encoded strings that don't pass as valid URI strings and cannot be passed as URL parameters.

What cipher, implemented in Ruby, is the best for this purpose?

+3


source to share


2 answers


Use Base64 urlsafe_encode64

Base64 is a good idea to use, Ruby provides a safe URL implementation because characters like + and = will be generated by default. urlsafe_encode64

will use '-' instead of '+' and '_' instead of '+' so that they can be passed as URL parameters.



require "base64"

encoded_url = Base64.urlsafe_encode64('http://www.someurl.com', ) #=> "aHR0cDovL3d3dy5zb21ldXJsLmNvbQ=="
decoded_url = Base64.urlsafe_decode64(encoded_url) #=> "http://www.someurl.com"

      

+7


source


If you really need to encode it and security isn't an issue, just use Base64

and then the URI will come out of it. But no need, just run away.

str = Base64.encode64(url)

encoded_str = URI.encode(str)



btw, this is a half-like duplicate

Ruby url encoding string

+1


source







All Articles