Created
April 26, 2017 04:04
-
-
Save mpkocher/ded8f2053ae5a04f8bd07c4d994e604d to your computer and use it in GitHub Desktop.
Example HMAC for Eve and SL Auth
This file contains hidden or 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
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