0

struggling to convert sha1 to base64

import hashlib
import hmac
import base64

base_string = "POST&https%3A%2F%2Fsecure.trademe.co.nz%2FOauth%2FRequestToken&oauth_callback%3Dhttp%253A%252F%252Fwww.website-tm-access.co.nz%252Ftrademe-callback%26oauth_consumer_key%3DC74CD73FDBE37D29BDD21BAB54BC70E422%26oauth_nonce%3D7O3kEe%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1285532322%26oauth_version%3D1.0%26scope%3DMyTradeMeRead%252CMyTradeMeWrite".encode()
KEY = b"3B61C8A3FF9F1F6C0DBE436F9B507E08EF&"
hmac.new(KEY, base_string, hashlib.sha1).base64()

AttributeError: 'HMAC' object has no attribute 'base64'

musss
  • 125
  • 1
  • 4
  • 16
  • Why did you assume the HMAC object had a `base64` method? [The docs](https://docs.python.org/3/library/hmac.html) don't show this. – taleinat Feb 21 '16 at 05:22
  • 2
    Possible duplicate of [Implementaion HMAC-SHA1 in python](http://stackoverflow.com/questions/8338661/implementaion-hmac-sha1-in-python) – taleinat Feb 21 '16 at 05:24

1 Answers1

3

I think you need to call base64.b64encode() on the digest:

import hashlib
import hmac
import base64

base_string = "POST&https%3A%2F%2Fsecure.trademe.co.nz%2FOauth%2FRequestToken&oauth_callback%3Dhttp%253A%252F%252Fwww.website-tm-access.co.nz%252Ftrademe-callback%26oauth_consumer_key%3DC74CD73FDBE37D29BDD21BAB54BC70E422%26oauth_nonce%3D7O3kEe%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1285532322%26oauth_version%3D1.0%26scope%3DMyTradeMeRead%252CMyTradeMeWrite".encode()
KEY = b"3B61C8A3FF9F1F6C0DBE436F9B507E08EF&"

digest = hmac.new(KEY, base_string, hashlib.sha1).digest()
print(base64.b64encode(digest))
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I honestly love you so much ! works like a charm :D :D :D :D :D :D :D :D :D so much relieffffffff – musss Feb 21 '16 at 08:03