Python实现HMACSHA1生成加密签名

2020-05-20 16:32:50 浏览数 (1)

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

代码语言:javascript复制
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 FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
mobile=18200000000&name=wangli
b976c851b1419f52eedfd3853f4f4d1c90ed3b24
 
Process finished with exit code 0

0 人点赞