Skip to content

Instantly share code, notes, and snippets.

@mwawrusch
Created November 19, 2013 03:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwawrusch/7539770 to your computer and use it in GitHub Desktop.
Save mwawrusch/7539770 to your computer and use it in GitHub Desktop.
#tested under ruby 1.9.2
#gem install ruby-hmac
require 'hmac-sha1'
require 'base64'
def make_signature(uri_path, params, client_id, secret)
padding_factor = (4 - secret.length % 4) % 4
secret += "=" * padding_factor
secret = secret.gsub(/[-_]/, {"-" => "+", "_" => "/"})
binary_key = Base64.decode64(secret)
params.update({"client" => client_id})
path = uri_path + "?" + params.collect{|k,v| "#{k}=#{v}"}.inject{|initial,cur| initial + "&" + cur}
digest = HMAC::SHA1.new(binary_key).update(path).digest
digest = Base64.encode64(digest).gsub(/[+\/]/, {"+" => "-", "/" => "_"}).delete("=")
return "#{path}&sig=#{digest}"
end
#puts 'http://api.singleplatform.co' + make_signature('/locations/haru-7', {}, "cyvhg72sot1qwa6mr4bx5hr62", "JZT_2p2X6BoRukjqjiujvbzZTKn1jrnS-HLHe-BE0jc")
puts 'http://api.singleplatform.co' + make_signature('/locations/search', {"q" => "Haru%20Sushi", "page"=>0,"count"=>20}, "XXX", "XXXX")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment