Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HMAC/ Hashlib Tutorial Request
#1
I'm requesting a tutorial/ help on how to create a new HMAC using SHA512 to send a get request to Bittrex.com's API to place a buy/sell/etc order/request.

I know how to do requests that are public but am having a really tough time finding how to create a new HMAC.

The internet desperately needs a basic explanation on these two or three topics, because I have searched youtube, forums, google search results, modules' code on github, api documentations like on Bittrex itself, and even bought 3 courses on Udemy.

NOTHING is explaining the entire process of creating a new HMAC request.

I am gladly ready to send anyone who helps me learn how to do this a tip in Bitcoin, because I would appreciate help that much.

Or also just pointing me somewhere to learn about HMAC, hashlib, and anything else that I may be missing such as UTF-8 encoding, ASCII encoding, bytearray, or anything else that could be where I'm going wrong.

Just in case I was unclear, my goal here is this:

To be able to write something similar to this:

If bitcoin_price >= 10000:
then buy_bitcoin
Reply
#2
There's already one in the python documentation: https://docs.python.org/3.8/library/hashlib.html
Reply
#3
(Nov-17-2019, 04:02 PM)Larz60+ Wrote: There's already one in the python documentation: https://docs.python.org/3.8/library/hashlib.html

It's extremely unhelpful. There is no material explaining this to people like me who are new to Python.

For example, both the hmac and hashlib documentation show .hexdigest. So how am I supposed to know which needs to be used?

The documentations also are only for hmac individually and hashlib individually, not how to use hmac.new combined with hashlib.

I'm looking over the documentation of each trying to figure out how this fits together but I really don't think there is explanation there unless people already know how to use hmac and hashlib, especially together.

More example questions that those documentations don't answer:

get.request or post.request? I am very sure get.request is used but have seen some things saying to use post.request.

UTF-8 encoding? Is that used and if so where and for what?

Byte or bytearray? Is that used and if so where and for what? These are two more things I have seen some things say to use in unclear and/or incomplete explanations. Also, byte, b, or bytearray?

Decode key and message as ASCII? Or no? If so how, where, and why/for what purpose?

Is a header for content-type=application/json needed? If so how, where, and why/for what purpose? Bittrex's API documentation only says that a header named apisign is needed that contains the hash. It says nothing about needing json in the headers.

Is base64.base64encode needed and if so how, where, and why/for what purpose?
Reply
#4
This is my best attempt so far:

bittrex_exchange = requests.get('https://api.bittrex.com/api/v1.1/public/getmarketsummaries').json()
    bittrex_public_key = '000000002345'
    bittrex_secret_key = 'eeerrtt554333'
    noncea = str(int(round(time.time()-1571111111)*10))
    #get_balances_url = ('https://api.bittrex.com/api/v1.1/account/getbalances?')


    url = ('https://api.bittrex.com/api/v1.1/account/getbalances')
    headers = 'apikey:'+(bittrex_public_key) + '&' + 'nonce:'+str(noncea)
    payload_parameters = {'apikey':(bittrex_public_key), 'nonce':(noncea)}
    message1 = url + headers
    #m2 = url + payload_parameters
    m3 = url + headers
    #m4 = url + payload_parameters
    #m5 = url + json.dumps(headers)
    #m6 = url + json.dumps(payload_parameters)
    m7 = url + headers
    #m8 = url + payload_parameters
    key_secret= bittrex_secret_key
    digestmod= hashlib.sha512
    hmacc= hmac.new(key= bytearray(key_secret), msg= bytearray(message1), digestmod=digestmod).hexdigest()
    API_request_header= {'apisign':hmacc}
    API_request= request.get(url, headers=headers, apisign=hmacc)
    print(hmacc)
    print(API_request)
Reply
#5
I move this thread to web development/web scrapping section as you looking for very particular help, not general tutorial on topic
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020