jmeter使用beanshell完成签名计算,附与python代码对比
2021-02-12 11:21
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 String hexB = Integer.toHexString(utfBytes[i]);
if (hexB.length() 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() 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+"");
文章标题:jmeter使用beanshell完成签名计算,附与python代码对比
文章链接:http://soscw.com/index.php/essay/54425.html