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?
source to share
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"
source to share