最近公司的接口都是MD5 16位加密,所以要使用加密功能。
之前也做过加密,因为用的比较少,所以是写了一个加密方法,导出JAR包,调用的。用起来需要很多设置,并且换算效率也不高。听前同事说,jmeter可以自己写函数,放在函数管理里面,今天自己就根据网上的大神分享的内容,简单写一下自己使用过程。
1、首先,eclipse 加载JMeter源码,部署好(不懂的可以看我前面的随笔)
2、找到functions,添加MD5_16/32类
3、粘贴32位MD5加密代码
package org.apache.jmeter.functions;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.AbstractFunction;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
/**
*
*/
@SuppressWarnings("deprecation")
public class MD5_32_Encipher extends AbstractFunction {
// 获取日志类
private static final Logger log = LoggingManager.getLoggerForClass();
private static final List<String> desc = new LinkedList<String>();
private static final String KEY = "__md5_32"; //$NON-NLS-1$
private static final int MAX_PARA_COUNT = 1;
private static final int MIN_PARA_COUNT = 1;
static {
desc.add("输入需要进行32md5加密的字符串!");
}
private String str = "";
private Object[] values;
public MD5_32_Encipher() {
}
@Override
public String execute(SampleResult arg0, Sampler arg1) throws InvalidVariableException {
// 函数执行逻辑,通过该方法取出参数
str = ((CompoundVariable) values[0]).execute();
log.info("传入的参数为:" + str);
str = encryption(str);
log.info("加密后为:" + str);
return str;
}
@Override
public String getReferenceKey() {
// 返回函数名称
return KEY;
}
@Override
public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
// 设置参数属性
checkParameterCount(parameters, MIN_PARA_COUNT, MAX_PARA_COUNT);
values = parameters.toArray();
}
@Override
public List<String> getArgumentDesc() {
// 返回参数描述
return desc;
}
// 32位小写md5加密算法
public String encryption(String plain) {
String re_md5 = new String();
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plain.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
re_md5 = buf.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return re_md5;
}
}
4、找到functions,添加MD5_16类 粘贴16位MD5加密代码
package org.apache.jmeter.functions;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.AbstractFunction;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
/**
*
*/
@SuppressWarnings("deprecation")
public class MD5_16_Encipher extends AbstractFunction {
// 获取日志类
private static final Logger log = LoggingManager.getLoggerForClass();
private static final List<String> desc = new LinkedList<String>();
private static final String KEY = "__md5_16"; //$NON-NLS-1$
private static final int MAX_PARA_COUNT = 1;
private static final int MIN_PARA_COUNT = 1;
static {
desc.add("输入需要进行16md5加密的字符串!");
}
private String str = "";
private Object[] values;
public MD5_16_Encipher() {
}
@Override
public String execute(SampleResult arg0, Sampler arg1) throws InvalidVariableException {
// 函数执行逻辑,通过该方法取出参数
str = ((CompoundVariable) values[0]).execute();
log.info("传入的参数为:" + str);
str = encryption(str);
log.info("加密后为:" + str);
return str;
}
@Override
public String getReferenceKey() {
// 返回函数名称
return KEY;
}
@Override
public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
// 设置参数属性
checkParameterCount(parameters, MIN_PARA_COUNT, MAX_PARA_COUNT);
values = parameters.toArray();
}
@Override
public List<String> getArgumentDesc() {
// 返回参数描述
return desc;
}
// 32位小写md5加密算法
public String encryption(String plain) {
String re_md5 = new String();
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plain.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
re_md5 = buf.toString().substring(8, 24);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return re_md5;
}
}
5、导出functions,命名为:ApacheJMeter_functions.jar
6、复制这个文件到 JMETER目录\lib\ext 下,覆盖即可(如果对自己写的代码没有信心,建议备份原来的文件后再覆盖)
7、打开JMeter,检查函数助手对话框,OK出现了,赶快去试试吧!