标签:ade class 没有 toc 完成 shell 参数转换 har indent
签名计算过程:
1.ticket计算:时间戳加+随机数字拼接后md5加密
2.组装公共参数+ticket+时间戳+业务参数
beanshell代码实现:
import java.util.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.Date;
import java.text.SimpleDateFormat;
import net.sf.json.JSONObject;
//net.sf.json.JSONObject 将Map转换为JSON方法
//获取shop_id
String shop_id=vars.get("ebai_shopId");
//log.info("shop_id1233:"+shop_id+"");
//获取时间戳
long timestamp=new Date().getTime();
vars.put("timestamp",timestamp+"");
//log.info("timestamp:"+timestamp+"");
//中文转unicode编码
public static String gbEncoding( String gbString) {
char[] utfBytes = gbString.toCharArray();
String unicodeBytes = "";
for (int i = 0; i < utfBytes.length; i++) {
String hexB = Integer.toHexString(utfBytes[i]);
if (hexB.length() <= 2) {
hexB = "00" + hexB;
}
unicodeBytes = unicodeBytes + "\\u" + hexB;
}
return unicodeBytes;
}
//md5加密方法
private static String getMD5(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
// log.info("input:"+input+"");
byte[] messageDigest = md.digest(input.getBytes());
BigInteger number = new BigInteger(1, messageDigest);
String hashtext = number.toString(16);
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
return hashtext.toUpperCase();
} catch (NoSuchAlgorithmException e) {
throw null;
}
}
//获取ticket方法
public static String getTicket() {
final int random = new Random().nextInt(Integer.MAX_VALUE);
final String md5 = getMD5(String.valueOf(timestamp + random));
StringBuilder sb = new StringBuilder(md5);
// 在左起第8、12、16、20位字符后面添加减号,注意是针对md5
final StringBuilder result = sb.insert(8, "-").insert(13, "-").insert(18, "-").insert(23, "-");
return result.toString().toUpperCase();
}
//将时间戳转换为时间字符串年月日,时分秒
public static String stampToDate(String s){
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = new Long(s);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}
//获取时间:年月日时间秒
String timestamp_str=timestamp+"";
String time_str=stampToDate(timestamp_str);
vars.put("time_str",time_str);
////获取ticket
String ticket=getTicket();
//公共请求参数
String data_str="data=";
String pub_request_data="&source="+shop_id+"&ticket="+ticket+"×tamp="+timestamp_str+"";
String secret_key="111";
//获取商家信息
//处理请求参数
Map commercialInfo_map=new HashMap();
commercialInfo_map.put("shop_id", shop_id) ;
JSONObject commercialInfo_str = JSONObject.fromObject(commercialInfo_map);
//log.info("commercialInfo_str:"+commercialInfo_str+"");
//1.组装请求参数
String request_data_commercialInfo=""+data_str+commercialInfo_str+pub_request_data+"";
vars.put("request_data_commercialInfo",request_data_commercialInfo);
//log.info("request_data_commercialInfo:"+request_data_commercialInfo+"");
//2.拼接加密字符串
String commercialInfo_Authorization=""+request_data_commercialInfo+secret_key+"";
//log.info("Authorization_commercialInfo:"+Authorization_commercialInfo+"");
//3.获取加密后的签名
String Authorization_commercialInfo=getMD5(commercialInfo_Authorization);
vars.put("Authorization_commercialInfo",Authorization_commercialInfo);
//log.info("Authorization_commercialInfo:"+Authorization_commercialInfo+"");
python代码实现:
# ‘‘‘生成md5加密字符串‘‘‘
def md5_Encry(self, str_in,upper=None):
str_out = hashlib.md5() # 采用md5加密
str_out.update(str(str_in).encode(encoding=‘utf-8‘))
md5=str_out.hexdigest()
if upper==True:
return md5.upper()
return md5
def str_to_time(self, time_to_str=‘1970-01-01 00:00:00‘): # 字符串转换为时间戳
try:
if time_to_str==‘1970-01-01 00:00:00‘:
time_to_str=self.time_to_str()
d = datetime.datetime.strptime(time_to_str, "%Y-%m-%d %H:%M:%S")
t = d.timetuple()
timeStamp = int(time.mktime(t))
# print(timeStamp)
return timeStamp
except Exception as error:
print("数据处理失败,原因为:\n%s" % (error))
def get_timestamp(self): # 输出当前时间的13位时间戳
current_milli_time = lambda: int(round(time.time() * 1000)) # 输出13位时间戳,round:对浮点数进行四舍五入
return str(current_milli_time())
# ticket计算 def kuaishou_ticket_calc(self): random_int=random.randint(1,100) md5_string=self.requestData.md5_Encry(‘{}{}‘.format(self.timestamp,random_int),upper=True) list_md5=list(md5_string) list_md5.insert(8,‘-‘),list_md5.insert(13,‘-‘),list_md5.insert(18,‘-‘),list_md5.insert(23,‘-‘) ticket_str=‘‘.join(list_md5) # print(ticket_str) return ticket_str # # 授权参数计算 def kuaishou_sign_calc(self,sign_data=None): if sign_data==None: # 将参数转换为dict并排序 str_dict = self.requestData.strToDict(self.public_data, space_one=‘=‘, space_two=‘&‘) str_dict = self.requestData.sortedDict(str_dict) # 将dict转换为str sort_str = self.requestData.dictToStr(str_dict) self.data=‘data={}&{}‘.format(self.data,sort_str) input_auth_str = ‘{}{}‘.format(self.data,self.secret_key) else: input_auth_str=sign_data print(input_auth_str) auth_str=self.requestData.md5_Encry(input_auth_str,upper=True) print(auth_str) return auth_str # 获取商户信息与资质信息 def kuaishou_openshop_merchandiseInfo(self,data): url=self.yaml[‘gld_api_host‘]+‘/api/snack/open/kuaishou/commercial/info‘ self.data=json.dumps(data) self.headers[‘Authorization‘]=self.kuaishou_sign_calc() requets=self.request.run_main(‘post‘,url=url,data=self.data,headers=self.headers) print(json.dumps(requets,indent=2,ensure_ascii=False))
遇到得坑:中文需要进行unicode编码,不然一直提示签名错误,坑了我好久
计算签名时,python中的json.dumps方法会自动对json中得中文进行unicode编码,而beanshell中的hashmap转换为json对象时则不会,需要需要手动转换编码
不知道java中有没有类似json.dumps得方法,希望各位大佬支个招,能用更好得方式实现
jmeter使用beanshell完成签名计算,附与python代码对比
标签:ade class 没有 toc 完成 shell 参数转换 har indent
原文地址:https://www.cnblogs.com/qtclm/p/12731274.html