Created
September 24, 2014 15:30
-
-
Save turret-io/2ca70ff719c7be2028d2 to your computer and use it in GitHub Desktop.
Generate HMAC in Python
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
import hmac, hashlib, json, time, base64 | |
SHARED_SECRET = 'sup3rs3cr3t!!' | |
def signString(string_to_sign, shared_secret): | |
return hmac.new(shared_secret, string_to_sign, hashlib.sha512).digest() | |
if __name__ == '__main__': | |
payload = { | |
'name': 'joe smith', | |
'category': 'people', | |
'action': 'transport', | |
'where': 'pluto', | |
'timestamp': str(int(time.time())) | |
} | |
json_payload = json.dumps(payload) | |
signature = signString(json_payload, SHARED_SECRET) | |
encoded_signature = base64.urlsafe_b64encode(signature) | |
encoded_payload = base64.urlsafe_b64encode(json_payload) | |
print('/?data={}&signature={}').format(encoded_payload, encoded_signature) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment