Skip to content

Instantly share code, notes, and snippets.

@turret-io
Created September 24, 2014 15:30
Show Gist options
  • Save turret-io/2ca70ff719c7be2028d2 to your computer and use it in GitHub Desktop.
Save turret-io/2ca70ff719c7be2028d2 to your computer and use it in GitHub Desktop.
Generate HMAC in Python
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