Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HMAC signing requests in Python

I'm trying to create an HMAC-SHA512 signed request for an API call in Python 3.4 using the requests library. I'm trying to follow docs, but am hitting this error:

AttributeError: '_hashlib.HASH' object has no attribute 'new'

Here's some code. It's failing with the error on the hmac constructor. It's fine if I try and pass hashlib.md5() or omit the digest parameter entirely.

I'm not sure if I'm signing the request properly afterwards as I haven't got that far yet. The docs for the service I'm trying to use say to sign the URL with my secret. I need this to be a byte string for this to work.

import hmac
import hashlib
import requests

secret = b'mysecret'
url = b'http://someurl.com/something/'

signing = hmac.new(secret, url, hashlib.sha512())

headers = {'apisign': signing.digest()}
response = requests.get(url, headers=headers)

Any pointers appreciated. I couldn't find an example. Thanks!

like image 261
Ludo Avatar asked Jun 26 '14 11:06

Ludo


People also ask

How do I create a signature in Hmac?

Setting up HMAC using the DashboardScroll to the Authentication options. Select HMAC (Signed Authetication Key) from the drop-down list. Configure your HMAC Request Signing settings. Select Strip Authorization Data to strip any authorization data from your API requests.

What is Hmac API?

Introduction. HMAC (hash-based message authentication code) is used to verify that a request is coming from an expected source and that the request has not been tampered with in transit.


1 Answers

You must pass in a reference to the hashlib.sha512 callable, not the result of calling it:

signing = hmac.new(secret, url, hashlib.sha512)

Alternatively, you could just use the string 'sha512':

signing = hmac.new(secret, url, 'sha512')

and hashlib.new() will be used to construct the hash object.

Demo:

>>> import hashlib
>>> import hmac
>>> secret = b'mysecret'
>>> url = b'http://someurl.com/something/'
>>> signing = hmac.new(secret, url, hashlib.sha512)
>>> signing.digest()
b'!~s2\x97\x97\xa9\xcc\xefcb\xa8\xcc\xa7\xbc\xec\xe5\xfc\xc3\xac\xfc\xbc5]\x05\x96\xc7\x83\x8b\x1b\x90\xd3\xa5\xca\x8cLsC\x17\xa0\xea\xa3\xfe\xd8M\xfda\x1epj\x90\xff}\xfa\xc2@\x92\xfb\xee\xa8\xab\x1b\x08\x8e'
like image 71
Martijn Pieters Avatar answered Oct 12 '22 07:10

Martijn Pieters