Skip to content

Instantly share code, notes, and snippets.

@NZKoz
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NZKoz/8968692 to your computer and use it in GitHub Desktop.
Save NZKoz/8968692 to your computer and use it in GitHub Desktop.
def gimme_yo_stuff
time = Time.now.httpdate
YOUR HTTP LIBRARY.GET(path, :headers=> {
"Accept" => ACCEPT,
"Date"=>time,
"Authorization"=>"#{@key_id}:#{signature_for_path(path, 'GET', time)}"
})
end
def signature_for_path(path, method, time)
payload = string_to_sign(method, path, time)
digest = OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha256'), @secret, payload)
# this nonsense is base64 encoding, you may or may not have to remove the \n from your
# digests, depends how your library works
[digest].pack('m').gsub("\n", '')
end
def string_to_sign(method, path, time)
[method, HOST, path, time, @key_id, ACCEPT, ''].join("\n")
end
>>> payload = 'lulz'
>>> import hashlib
>>> import hmac
>>> import base64
>>> h = hmac.new("YOUR SECRET", payload, hashlib.sha256)
>>> h = hmac.new("YOUR SECRET", payload, hashlib.sha256)
>>> d = h.digest()
>>> base64.b64encode(d)
'AKdJyTNPtw+rFru9gl/pb6Wf5OBu/12ycZN/i+quCyU='
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment