Skip to content

Instantly share code, notes, and snippets.

@rcoup
Forked from NZKoz/companies.rb
Last active August 29, 2015 13:56

Revisions

  1. rcoup revised this gist Feb 13, 2014. 3 changed files with 32 additions and 29 deletions.
    32 changes: 32 additions & 0 deletions companies.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    import base64
    import hashlib
    import hmac
    import time
    from email.utils import formatdate
    import requests

    HOST="somewhere"
    KEY_ID="gimme"
    SECRET="ssshhh"
    ACCEPT="something"


    def gimme_yo_stuff(path):
    time = formatdate(localtime=True)
    requests.get(path, headers={
    "Accept": ACCEPT,
    "Date": time,
    "Authorization": "%s:%s" % (KEY_ID, signature_for_path(path, 'GET', time),
    })


    def signature_for_path(path, method, time):
    payload = string_to_sign(method, path, time)
    h = hmac.new(SECRET, payload, hashlib.sha256)
    d = h.digest()
    return base64.b64encode(d)

    def string_to_sign(method, path, time):
    return "\n".join([method, HOST, path, time, KEY_ID, ACCEPT, ''])

    # then call gimme_yo_stuff('/some/path')?
    20 changes: 0 additions & 20 deletions companies.rb
    Original file line number Diff line number Diff line change
    @@ -1,20 +0,0 @@
    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
    9 changes: 0 additions & 9 deletions hmac.py
    Original file line number Diff line number Diff line change
    @@ -1,9 +0,0 @@
    >>> 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='
  2. @NZKoz NZKoz revised this gist Feb 13, 2014. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions hmac.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    >>> 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='
  3. @NZKoz NZKoz revised this gist Feb 13, 2014. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions companies.rb
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,10 @@ def gimme_yo_stuff

    def signature_for_path(path, method, time)
    payload = string_to_sign(method, path, time)

    [OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha256'), @secret, payload)].pack('m').gsub("\n", '')
    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)
  4. @NZKoz NZKoz created this gist Feb 13, 2014.
    18 changes: 18 additions & 0 deletions companies.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    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)

    [OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha256'), @secret, payload)].pack('m').gsub("\n", '')
    end

    def string_to_sign(method, path, time)
    [method, HOST, path, time, @key_id, ACCEPT, ''].join("\n")
    end