Purpose to achieve:
Each request of the app contains a sign parameter in the request header.
His value is generated by developing a splicing method designed by himself and then encrypted by md5
We just want to generate the value of this sign
Prepare:
and develop the formula to be composed into the signature:
Write the java function of md5 with BeanShell:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
String MD5(String s) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(s.getBytes("utf-8"));
return toHex(bytes);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
String toHex(byte[] bytes) {
final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();
StringBuilder ret = new StringBuilder(bytes.length * 2);
for (int i=0; i<bytes.length; i++) {
ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
}
return ret.toString();
}
String params = "version=1";
String md5_src= params+vars.get("uid")+vars.get("token")+vars.get("app_version")+vars.get("timestamp")+vars.get("sign_salt");
//log.info("md5_src:"+md5_src);
String result = MD5(md5_src);
//log.info("md5:"+result);
vars.put("sign",result);
Final composition:
The source code and this jmeter file exist in my Baidu cloud disk.