背景:项目上需要对字符串MD5 32位加密,jmeter自带的为16位的 无法满足需要
主要参考搬运地址:https://www.cnblogs.com/allen-zml/p/6911944.html
准备工具:eclipse (我的版本4.7)
eclipse启动失败的小插曲,使用命令C:\Users\wangxu\eclipse\java-oxygen2\eclipse\eclipse -clean后解决
jmeter源码下载链接:https://archive.apache.org/dist/jmeter/source/
apache-jmeter-2.13_src.zip(本次使用的是2.13版本,下载后解压)
要把大象装进冰箱,主要分两步:
1.eclipse 加载JMeter源码
这里面主要碰到的坑是缺少依赖的外部jar包的问题
链接: https://pan.baidu.com/s/16xnPbP7F879p2PIhKoT9xw 提取码: sr94
一次性下载好,不收费
打开eclipse,file-new-javaproject
next
finish
关掉eclipse
找到工作目录,删除里面的bin 、src两个文件夹
文本工具(UE等)打开源码文件夹的eclipse.classpath, 复制其中内容,替换掉工作目录的.classpath里面的内容
重新打开eclipse
项目名称(这里jmeter2.13)上右键 import
选择File System
点击select all, finish
导入完成后,复制下载的61个jar包,放入eclipse-workspace\jmeter2.13\lib 里
项目名称,右键,refresh,发现还有3个包指定的路径不对
在eclipse-workspace\jmeter2.13\lib 下 新建api文件夹,放入bcmail/bcpkix/bcprov 开头的这三个包(已经在eclipse-workspace\jmeter2.13\lib里了),继续项目名称,右键,refresh
仅剩警告,不调了。
2.搬运代码,导出jar包使用
如图打开目录,在最后的functions里,右键new-class两次,名称分别为MD5_32_Encipher
MD5_16_Encipher
清除掉里面内容,代码搬运
1)粘贴32位MD5加密代码(MD5_32_Encipher.java)
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;
}
}
2)粘贴16位MD5加密代码(MD5_16_Encipher )
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;
}
}