2023-03-28 09:54:53 +08:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# encoding: utf-8
|
|
|
|
|
"""
|
|
|
|
|
@author: kira
|
|
|
|
|
@contact: 262667641@qq.com
|
2023-08-01 17:38:04 +08:00
|
|
|
|
@file: rules.py
|
2023-03-28 09:54:53 +08:00
|
|
|
|
@time: 2023/3/14 16:21
|
2023-08-01 17:38:04 +08:00
|
|
|
|
@desc:自定义加密规则
|
2023-03-28 09:54:53 +08:00
|
|
|
|
"""
|
|
|
|
|
import json
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
from natsort import natsorted
|
|
|
|
|
|
2023-09-04 10:24:49 +08:00
|
|
|
|
__all__ = ["md5_sign", "sha1_sign", "rsa_sign"]
|
2023-03-28 09:54:53 +08:00
|
|
|
|
|
2023-06-13 17:08:05 +08:00
|
|
|
|
from common.crypto.encryption_str import sha1_secret_str, md5
|
2023-09-04 10:24:49 +08:00
|
|
|
|
from common.crypto.encryption_rsa import Rsa
|
2023-03-28 09:54:53 +08:00
|
|
|
|
|
|
|
|
|
|
2023-09-04 10:24:49 +08:00
|
|
|
|
def md5_sign(post_data: dict):
|
2023-07-04 18:05:35 +08:00
|
|
|
|
"""
|
2023-09-04 10:24:49 +08:00
|
|
|
|
MD5 数据加签
|
2023-07-04 18:05:35 +08:00
|
|
|
|
Args:
|
2023-08-08 18:10:58 +08:00
|
|
|
|
**data:需要加钱的数据DD
|
2023-07-04 18:05:35 +08:00
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
2023-09-04 10:24:49 +08:00
|
|
|
|
print(f"开始执行md5_sign加密,待加密参数={post_data}")
|
2023-07-04 18:05:35 +08:00
|
|
|
|
sorted_list = []
|
2023-09-04 10:24:49 +08:00
|
|
|
|
for key, value in post_data.items():
|
2023-07-04 18:05:35 +08:00
|
|
|
|
try:
|
|
|
|
|
sorted_params = str(key) + str(value)
|
|
|
|
|
sorted_list.append(sorted_params)
|
|
|
|
|
except Exception:
|
|
|
|
|
raise
|
|
|
|
|
sort = natsorted(sorted_list) # 列表自然排序
|
|
|
|
|
argument = "加签所需要的密钥"
|
|
|
|
|
keystore = argument + ("".join(sort)) # 生成加带密钥的新字符串
|
|
|
|
|
sign_value = md5(keystore)
|
2023-09-04 10:24:49 +08:00
|
|
|
|
return {**post_data, **{"sign": sign_value}}
|
2023-03-28 09:54:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sha1_sign(post_data: dict):
|
2023-09-04 10:24:49 +08:00
|
|
|
|
"""sha1 加密"""
|
|
|
|
|
print(f"开始执行sha1_sign加密,待加密参数={post_data}")
|
2023-07-04 18:05:35 +08:00
|
|
|
|
timestamp = int(round(time.time() * 1000)) # 毫秒级时间戳
|
2023-09-04 10:24:49 +08:00
|
|
|
|
argument = {"secretKey": "xxxx", "timestamp": timestamp} # 加密加盐参数
|
2023-07-04 18:05:35 +08:00
|
|
|
|
res = {**post_data, **argument}
|
|
|
|
|
sorted_list = []
|
|
|
|
|
for key, value in res.items():
|
|
|
|
|
try:
|
|
|
|
|
value = json.dumps(value, ensure_ascii=False) if isinstance(value, list) or isinstance(value,
|
|
|
|
|
dict) else str(
|
|
|
|
|
value)
|
|
|
|
|
sorted_params = str(key) + "=" + value
|
|
|
|
|
sorted_list.append(sorted_params)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise e
|
|
|
|
|
sort = natsorted(sorted_list) # 列表自然排序 返回[(key,value),(key,value)]
|
|
|
|
|
splicing_str = "&".join(sort) # 排序结果 key=value&key=value 拼接
|
|
|
|
|
encrypted_str = sha1_secret_str(splicing_str) # sha1加密
|
|
|
|
|
sign = {"signature": encrypted_str}
|
|
|
|
|
return {**res, **sign} # request 中使用json入参,则传dict
|
2023-09-04 10:24:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rsa_sign(post_data):
|
|
|
|
|
"""rsa 加密"""
|
|
|
|
|
rs = Rsa(post_data)
|
|
|
|
|
return rs.rsa_encrypt()
|