[英]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"尝试为 3Commas 生成 HMAC SHA256 签名,我使用官方示例中的相同参数,它应该生成:“30f678a157230290e00475cfffccbc92ae3659d94c145a2c0e9d0fa28f41c11a”
But I generate: "17a656c7df48fa2db615bfc719627fc94e59265e6af18cc7714694ea5b58a11a"但我生成:“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?谁能帮我吗?
Try using the hmac
module instead of the hashlib
module:尝试使用hmac
模块而不是hashlib
模块:
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
I did implement it using a different approach few years ago.几年前我确实使用不同的方法实现了它。
import hmac
import hashlib
import binascii
def create_sha256_signature(key, message):
byte_key = binascii.unhexlify(key)
message = message.encode()
return hmac.new(byte_key, message, hashlib.sha256).hexdigest().upper()
create_sha256_signature("E49756B4C8FAB4E48222A3E7F3B97CC3", "TEST STRING")
https://www.gauravvjn.com/generate-hmac-sha256-signature-in-python/ https://www.gauravvjn.com/generate-hmac-sha256-signature-in-python/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.