Python使用HMACSHA1加密生成签名

  • Post author:
  • Post category:python


按key升序,key不为sign,value不为空,再把key和value拼装成x=a&y=b格式,然后使用HmacSHA1算法生成签名

import json,hashlib,hmac,base64

def sign(data):
    date_new=''
    for key,value in sorted(data.items()):
        if key!="sign" and value:
            date_new+= key + "=" +str(value)+"&"
    sk="wwwsss123"
    #生成签名
    hashing=hmac.new(bytes(sk,encoding='utf-8'),bytes(date_new.strip('&'),encoding='utf-8'),hashlib.sha1).hexdigest()
    print(date_new.strip('&'))
    return hashing
data={"name":"wangli","mobile":18200000000,"sn":"","sign":""}
print(sign(data))




"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
mobile=18200000000&name=wangli
b976c851b1419f52eedfd3853f4f4d1c90ed3b24

Process finished with exit code 0



版权声明:本文为qq_36502272原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。