Created
November 19, 2013 03:20
-
-
Save mwawrusch/7539770 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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