parent
8940d23028
commit
d3d930b001
4 changed files with 99 additions and 1 deletions
@ -0,0 +1,46 @@ |
||||
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 |
Loading…
Reference in new issue