Skip to content

Instantly share code, notes, and snippets.

@mpkocher
Created April 26, 2017 04:04
Show Gist options
  • Save mpkocher/ded8f2053ae5a04f8bd07c4d994e604d to your computer and use it in GitHub Desktop.
Save mpkocher/ded8f2053ae5a04f8bd07c4d994e604d to your computer and use it in GitHub Desktop.
Example HMAC for Eve and SL Auth
import hashlib
import hmac
import math
import datetime
import base64
def to_h(secret, method, segment, t=None):
"""
Example:
In [1]: to_h("s", "GET", "/api/v1/files")
Out[1]: u'cXgVkgiUxqS/02QnhZi2PHQHPwLR8A9tceqsO9nXNAo='
"""
if t is None:
d = datetime.datetime.now()
n = math.pow(10, 2)
t = math.floor(long(d.strftime("%s")) / n)
x = "+".join([method, segment, str(int(t))])
digest = hmac.new(secret, x, digestmod=hashlib.sha256).digest()
return base64.b64encode(digest).decode()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment