You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.1 KiB
46 lines
1.1 KiB
import re |
|
from hashlib import md5 |
|
|
|
""" |
|
用于生成网售签名 |
|
""" |
|
|
|
|
|
class Sign: |
|
|
|
def __init__(self, salt='ly725xw701'): |
|
self.salt = salt |
|
|
|
def __ksort(self, params): |
|
""" |
|
请求参数排序 |
|
:params: 未排序的参数字典 |
|
:return: 排列后的请求参数字符串 |
|
""" |
|
sorted_params = sorted(params.items(), key=lambda k: k[0]) |
|
p = [f'{k}={v}' for k, v in sorted_params] |
|
params_str = '&'.join(p) |
|
return params_str |
|
|
|
def __generate_sign(self, params): |
|
""" |
|
生成签名方法 |
|
:param params: 未排序的参数字典 |
|
:param salt: 盐值(授权码) |
|
:return: 生成签名 |
|
""" |
|
str0 = self.salt + self.__ksort(params) |
|
m1 = md5(str0.encode()).hexdigest() |
|
str1 = m1 + self.salt |
|
sign = md5(str1.encode()).hexdigest() |
|
return sign |
|
|
|
def add_sig(self, params): |
|
""" |
|
添加sig到params |
|
:param params: |
|
:return: |
|
""" |
|
sig = self.__generate_sign(params) |
|
params.update({'_sig': sig}) |
|
return params |