package md5package;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; public class String2md5 { public static void main(String args[]) {
String aaa=StringToMd5("abc"); if(aaa.equals("900150983cd24fb0d6963f7d28e17f72"))
System.out.println("MD5加密后的值是:"+aaa);
else
System.out.println("-----sorry-"+aaa); } public static String StringToMd5(String psw) {
{
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(psw.getBytes("UTF-8"));
byte[] encryption = md5.digest(); StringBuffer strBuf = new StringBuffer();
for (int i = 0; i < encryption.length; i++) {
if (Integer.toHexString(0xff & encryption[i]).length() == 1) {
strBuf.append("0").append(Integer.toHexString(0xff & encryption[i]));
} else {
strBuf.append(Integer.toHexString(0xff & encryption[i]));
}
} return strBuf.toString();
} catch (NoSuchAlgorithmException e) {
return "";
} catch (UnsupportedEncodingException e) {
return "";
}
}
} }

  找了一下,目前Jmeter4.0版本中已经默认自带这个md5方法了,源码如下:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package org.apache.commons.codec.digest; import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.binary.StringUtils; public class DigestUtils {
private static final int STREAM_BUFFER_LENGTH = 1024;
private final MessageDigest messageDigest; public static byte[] digest(MessageDigest messageDigest, byte[] data) {
return messageDigest.digest(data);
} public static byte[] digest(MessageDigest messageDigest, ByteBuffer data) {
messageDigest.update(data);
return messageDigest.digest();
} public static byte[] digest(MessageDigest messageDigest, File data) throws IOException {
return updateDigest(messageDigest, data).digest();
} public static byte[] digest(MessageDigest messageDigest, InputStream data) throws IOException {
return updateDigest(messageDigest, data).digest();
} public static MessageDigest getDigest(String algorithm) {
try {
return MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException var2) {
throw new IllegalArgumentException(var2);
}
} public static MessageDigest getDigest(String algorithm, MessageDigest defaultMessageDigest) {
try {
return MessageDigest.getInstance(algorithm);
} catch (Exception var3) {
return defaultMessageDigest;
}
} public static MessageDigest getMd2Digest() {
return getDigest("MD2");
} public static MessageDigest getMd5Digest() {
return getDigest("MD5");
} public static MessageDigest getSha1Digest() {
return getDigest("SHA-1");
} public static MessageDigest getSha256Digest() {
return getDigest("SHA-256");
} public static MessageDigest getSha384Digest() {
return getDigest("SHA-384");
} public static MessageDigest getSha512Digest() {
return getDigest("SHA-512");
} /** @deprecated */
@Deprecated
public static MessageDigest getShaDigest() {
return getSha1Digest();
} public static byte[] md2(byte[] data) {
return getMd2Digest().digest(data);
} public static byte[] md2(InputStream data) throws IOException {
return digest(getMd2Digest(), data);
} public static byte[] md2(String data) {
return md2(StringUtils.getBytesUtf8(data));
} public static String md2Hex(byte[] data) {
return Hex.encodeHexString(md2(data));
} public static String md2Hex(InputStream data) throws IOException {
return Hex.encodeHexString(md2(data));
} public static String md2Hex(String data) {
return Hex.encodeHexString(md2(data));
} public static byte[] md5(byte[] data) {
return getMd5Digest().digest(data);
} public static byte[] md5(InputStream data) throws IOException {
return digest(getMd5Digest(), data);
} public static byte[] md5(String data) {
return md5(StringUtils.getBytesUtf8(data));
} public static String md5Hex(byte[] data) {
return Hex.encodeHexString(md5(data));
} public static String md5Hex(InputStream data) throws IOException {
return Hex.encodeHexString(md5(data));
} public static String md5Hex(String data) {
return Hex.encodeHexString(md5(data));
} /** @deprecated */
@Deprecated
public static byte[] sha(byte[] data) {
return sha1(data);
} /** @deprecated */
@Deprecated
public static byte[] sha(InputStream data) throws IOException {
return sha1(data);
} /** @deprecated */
@Deprecated
public static byte[] sha(String data) {
return sha1(data);
} public static byte[] sha1(byte[] data) {
return getSha1Digest().digest(data);
} public static byte[] sha1(InputStream data) throws IOException {
return digest(getSha1Digest(), data);
} public static byte[] sha1(String data) {
return sha1(StringUtils.getBytesUtf8(data));
} public static String sha1Hex(byte[] data) {
return Hex.encodeHexString(sha1(data));
} public static String sha1Hex(InputStream data) throws IOException {
return Hex.encodeHexString(sha1(data));
} public static String sha1Hex(String data) {
return Hex.encodeHexString(sha1(data));
} public static byte[] sha256(byte[] data) {
return getSha256Digest().digest(data);
} public static byte[] sha256(InputStream data) throws IOException {
return digest(getSha256Digest(), data);
} public static byte[] sha256(String data) {
return sha256(StringUtils.getBytesUtf8(data));
} public static String sha256Hex(byte[] data) {
return Hex.encodeHexString(sha256(data));
} public static String sha256Hex(InputStream data) throws IOException {
return Hex.encodeHexString(sha256(data));
} public static String sha256Hex(String data) {
return Hex.encodeHexString(sha256(data));
} public static byte[] sha384(byte[] data) {
return getSha384Digest().digest(data);
} public static byte[] sha384(InputStream data) throws IOException {
return digest(getSha384Digest(), data);
} public static byte[] sha384(String data) {
return sha384(StringUtils.getBytesUtf8(data));
} public static String sha384Hex(byte[] data) {
return Hex.encodeHexString(sha384(data));
} public static String sha384Hex(InputStream data) throws IOException {
return Hex.encodeHexString(sha384(data));
} public static String sha384Hex(String data) {
return Hex.encodeHexString(sha384(data));
} public static byte[] sha512(byte[] data) {
return getSha512Digest().digest(data);
} public static byte[] sha512(InputStream data) throws IOException {
return digest(getSha512Digest(), data);
} public static byte[] sha512(String data) {
return sha512(StringUtils.getBytesUtf8(data));
} public static String sha512Hex(byte[] data) {
return Hex.encodeHexString(sha512(data));
} public static String sha512Hex(InputStream data) throws IOException {
return Hex.encodeHexString(sha512(data));
} public static String sha512Hex(String data) {
return Hex.encodeHexString(sha512(data));
} /** @deprecated */
@Deprecated
public static String shaHex(byte[] data) {
return sha1Hex(data);
} /** @deprecated */
@Deprecated
public static String shaHex(InputStream data) throws IOException {
return sha1Hex(data);
} /** @deprecated */
@Deprecated
public static String shaHex(String data) {
return sha1Hex(data);
} public static MessageDigest updateDigest(MessageDigest messageDigest, byte[] valueToDigest) {
messageDigest.update(valueToDigest);
return messageDigest;
} public static MessageDigest updateDigest(MessageDigest messageDigest, ByteBuffer valueToDigest) {
messageDigest.update(valueToDigest);
return messageDigest;
} public static MessageDigest updateDigest(MessageDigest digest, File data) throws IOException {
BufferedInputStream stream = new BufferedInputStream(new FileInputStream(data)); MessageDigest var3;
try {
var3 = updateDigest(digest, (InputStream)stream);
} finally {
stream.close();
} return var3;
} public static MessageDigest updateDigest(MessageDigest digest, InputStream data) throws IOException {
byte[] buffer = new byte[1024]; for(int read = data.read(buffer, 0, 1024); read > -1; read = data.read(buffer, 0, 1024)) {
digest.update(buffer, 0, read);
} return digest;
} public static MessageDigest updateDigest(MessageDigest messageDigest, String valueToDigest) {
messageDigest.update(StringUtils.getBytesUtf8(valueToDigest));
return messageDigest;
} public static boolean isAvailable(String messageDigestAlgorithm) {
return getDigest(messageDigestAlgorithm, (MessageDigest)null) != null;
} /** @deprecated */
@Deprecated
public DigestUtils() {
this.messageDigest = null;
} public DigestUtils(MessageDigest digest) {
this.messageDigest = digest;
} public DigestUtils(String name) {
this(getDigest(name));
} public MessageDigest getMessageDigest() {
return this.messageDigest;
} public byte[] digest(byte[] data) {
return updateDigest(this.messageDigest, data).digest();
} public byte[] digest(String data) {
return updateDigest(this.messageDigest, data).digest();
} public byte[] digest(ByteBuffer data) {
return updateDigest(this.messageDigest, data).digest();
} public byte[] digest(File data) throws IOException {
return updateDigest(this.messageDigest, data).digest();
} public byte[] digest(InputStream data) throws IOException {
return updateDigest(this.messageDigest, data).digest();
} public String digestAsHex(byte[] data) {
return Hex.encodeHexString(this.digest(data));
} public String digestAsHex(String data) {
return Hex.encodeHexString(this.digest(data));
} public String digestAsHex(ByteBuffer data) {
return Hex.encodeHexString(this.digest(data));
} public String digestAsHex(File data) throws IOException {
return Hex.encodeHexString(this.digest(data));
} public String digestAsHex(InputStream data) throws IOException {
return Hex.encodeHexString(this.digest(data));
}
}

  Jmeter要如何引用呢

import org.apache.commons.codec.digest.DigestUtils;  

String str = "abc";
String str2 = "测试"; String sign = DigestUtils.md5Hex(str);
String sign2 = DigestUtils.md5Hex(str2);
String random = str;
String random2 = str2; vars.put("sign_str",sign.toUpperCase());
vars.put("random",random); vars.put("sign_str",sign2.toUpperCase());
vars.put("random",random2);

  

Java语言编写MD5加密方法,Jmeter如何给字符串MD5加密的更多相关文章

  1. Atiitt 使用java语言编写sql函数或存储过程

    Atiitt 使用java语言编写sql函数或存储过程 1.1. java编写sql函数或存储过程的机制1 1.2. Java编写sp的优点1 1.3. 支持java源码,class文件,blog f ...

  2. Fastjson是一个Java语言编写的高性能功能完善的JSON库。

    简介 Fastjson是一个Java语言编写的高性能功能完善的JSON库. 高性能 fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jackson. ...

  3. 使用Java语言编写一个五子棋UI界面并实现网络对战功能(非局域网)

    使用Java语言编写一个五子棋UI界面并实现网络对战功能(非局域网) 一,前期准备 1,Java IDE(Eclipse)与JDK的安装与配置jdk-15.0.1-免配置路径版提取码:earu免安装版 ...

  4. 使用java语言编写窗口按钮

    使用java语言编写窗口按钮 代码如下: package Day08; import java.awt.FlowLayout; import javax.swing.JButton;import ja ...

  5. java语言规范,main方法必须声明为public

    注释: 根据java语言规范,main方法必须声明为public. 当main方法不是public时,有些版本的java解释器也可以执行java应用程序.有个程序员报告了这个bug. 如果感兴趣可以查 ...

  6. 第二次作业利用java语言编写计算器进行四则运算

    随着第一次作业的完成,助教 牛老师又布置了第二次作业:用java语言编写一个程序然后进行四则运算用户用键盘输入一个字符来结束程序显示统计结果.一开始看到这个题目我也着实吓了一跳 因为不知道如何下手而且 ...

  7. 用Java语言编写一个简易画板

    讲了三篇概博客的概念,今天,我们来一点实际的东西.我们来探讨一下如何用Java语言,编写一块简易的画图板. 一.需求分析 无论我们使用什么语言,去编写一个什么样的项目,我们的第一步,总是去分析这个项目 ...

  8. java语言编写矩阵的四则运算

    题目要求如下: 设计程序实现矩阵的四则运算 设计要求: (1) 实现矩阵的四则运算. (2) 考虑实现带变元的矩阵计算. (3)考虑实现矩阵的特征值和特征向量的计算. 我使用java语言写的 目录结构 ...

  9. java基础知识回顾之---java String final类普通方法的应用之字符串数组排序

    /* * 1,给定一个字符串数组.按照字典顺序进行从小到大的排序. * {"nba","abc","cba","zz", ...

随机推荐

  1. racle修改字段类型时报"要更改的列必须为空"处理方法

    执行以下语句报"要修改数据类型,则要更改的列必须为空"      alter table 表名 modify (目标字段 varchar2(100)); 解决步骤: 第一步,在表中 ...

  2. [LeetCode] 383. Ransom Note_Easy tag: Hash Table

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

  3. Java-二叉树算法

    二叉树算法的排序规则: 1.选择第一个元素作为根节点 2.之后如果元素大于根节点放在右子树,如果元素小于根节点,则放在左子树 3.最后按照中序遍历的方式进行输出,则可以得到排序的结果(左->根- ...

  4. Python常用函数及说明

    原文地址:博客园  CSDN 基本定制型C.__init__(self[, arg1, ...]) 构造器(带一些可选的参数)C.__new__(self[, arg1, ...]) 构造器(带一些可 ...

  5. http协议基础(七)通用首部字段

    通用首部字段的意思,就是:请求和响应报文双方都会使用的首部 1.Cache-Control 通过指定它的指令,能操作缓存的工作机制 指令参数是可选的,多个指令通过“,”分隔 Cache-Control ...

  6. Summary: rand5构造rand7

    给一个方法,比如 rand5(), 它能够等概率生成 1-5 之间的整数. 所谓等概率就是1,2,3,4,5 生产的概率均为 0.2 .现在利用rand5(), 构造一个能够等概率生成 1- 7 的方 ...

  7. Apache+php+mysql环境配置

    Apache+PHP+MySQL环境搭建 标题格式 正文格式 阶段性完成格式 正文中强调格式 ————————————————————————————— 前语:本文是从我写过的doc文档迁移过来的,由 ...

  8. Lower Power with CPF(三)

    常用的一些Lower Power的策略: 1)Clock tree optimization and clock gating:在正常情况下clock信号会一直toggle at the maximu ...

  9. input/radio/select等标签的值获取和赋值

    input/radio/select等标签的值获取和赋值,这几个是使用率最高的几个标签,获取值和赋值以及初始化自动填充数据和选择: 页面html: <div class=" " ...

  10. python服务器端、客户端的模型,客服端发送请求,服务端进行响应(web.py)

    服务器端.客户端的模型,客服端发送的请求,服务端的响应 相当于启动了一个web server install web.py 接口框架用到的包 http://webpy.org/tutorial3.zh ...