Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate HMAC SHA256 signature Python

Trying to generate HMAC SHA256 signature for 3Commas, I use the same parameters from the official example, it should generate: "30f678a157230290e00475cfffccbc92ae3659d94c145a2c0e9d0fa28f41c11a"

But I generate: "17a656c7df48fa2db615bfc719627fc94e59265e6af18cc7714694ea5b58a11a"

Here is what I tried:

secretkey = 'NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j'
totalParams = '/public/api/ver1/accounts/new?type=binance&name=binance_account&api_key=XXXXXX&secret=YYYYYY'
print 'signature = '+hashlib.sha256((secretkey+totalParams).encode('ASCII')).hexdigest()

Can anyone help me out?

like image 374
Sky Avatar asked Dec 24 '18 08:12

Sky


People also ask

How do I get the HMAC SHA256 key?

First, enter the plain-text and the cryptographic key to generate the code. Then, you can use select the hash function you want to apply for hashing. The default is SHA-256. Then you can submit your request by clicking on the compute hash button to generate the HMAC authentication code for you.

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 SHA256 signature?

HMAC-SHA256 is an algorithm defined by RFC 2104 (RFC 2104—Keyed-Hashing for Message Authentication). The algorithm takes two byte-strings as input: a key and a message. The output of HMAC-SHA256 is a byte string, called the digest. You must perform the Base64 encoding of this digest to calculate the signature.


1 Answers

Try using the hmac module instead of the hashlib module:

import hmac
import hashlib
secret_key = b"NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j"
total_params = b"/public/api/ver1/accounts/new?type=binance&name=binance_account&api_key=XXXXXX&secret=YYYYYY"
signature = hmac.new(secret_key, total_params, hashlib.sha256).hexdigest()
print("signature = {0}".format(signature))

This gives the desired result:

signature = 30f678a157230290e00475cfffccbc92ae3659d94c145a2c0e9d0fa28f41c11a
like image 67
Stuart Wakefield Avatar answered Sep 19 '22 14:09

Stuart Wakefield