hex
有的接口认证,需要加密HMAC_SHA1
,输出加密后的hex
值HmacUtil.java
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class HmacUtil {
//加密算法
public static final String HMAC_SHA1 = "HmacSHA1";
public static final String HMAC_MD5 = "HmacMD5";
public static final String HMAC_SHA256 = "HmacSHA256";
public static final String HMAC_SHA512 = "HmacSHA512";
/**
* 实现Hmac系列的加密算法HmacSHA1、HmacMD5等
*
* @param input 需要加密的输入参数
* @param key 密钥
* @param algorithm 选择加密算法
* @return 加密后的值
**/
public static String encrypt(String input, String key, String algorithm) {
String cipher = "";
try {
byte[] data = key.getBytes(StandardCharsets.UTF_8);
//根据给定的字节数组构造一个密钥,第二个参数指定一个密钥的算法名称,生成HmacSHA1专属密钥
SecretKey secretKey = new SecretKeySpec(data, algorithm);
//生成一个指定Mac算法的Mac对象
Mac mac = Mac.getInstance(algorithm);
//用给定密钥初始化Mac对象
mac.init(secretKey);
byte[] text = input.getBytes(StandardCharsets.UTF_8);
byte[] encryptByte = mac.doFinal(text);
cipher = bytesToHexStr(encryptByte);
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
e.printStackTrace();
}
return cipher;
}
/**
* byte数组转16进制字符串
*
* @param bytes byte数组
* @return hex字符串
*/
public static String bytesToHexStr(byte[] bytes) {
StringBuilder hexStr = new StringBuilder();
for (byte b : bytes) {
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
hexStr.append(hex);
}
return hexStr.toString();
}
}
打包成jar包
$ javac HmacUtil.java
$ jar cvf HmacUtil.jar HmacUtil.class
已添加清单
正在添加: HmacUtil.class(输入 = 1810) (输出 = 1010)(压缩了 44%)
jmeter
中引入jar
包BeanShell
添加以下代码,并设置变量sign
String sign = HmacUtil.encrypt("abcdefg", "g1LsvbcQU2D4", HmacUtil.HMAC_SHA1);
vars.put("sign",sign);
与在线加密工具比对加密后的内容 http://www.wetools.com/hmac
base64
如果你需要得到base64的值,可以更改一下代码
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class HmacUtil {
//加密算法
public static final String HMAC_SHA1 = "HmacSHA1";
public static final String HMAC_MD5 = "HmacMD5";
public static final String HMAC_SHA256 = "HmacSHA256";
public static final String HMAC_SHA512 = "HmacSHA512";
/**
* 实现Hmac系列的加密算法HmacSHA1、HmacMD5等
*
* @param input 需要加密的输入参数
* @param key 密钥
* @param algorithm 选择加密算法
* @return 加密后的值
**/
public static String encrypt(String input, String key, String algorithm) {
String cipher = "";
try {
byte[] data = key.getBytes(StandardCharsets.UTF_8);
//根据给定的字节数组构造一个密钥,第二个参数指定一个密钥的算法名称,生成HmacSHA1专属密钥
SecretKey secretKey = new SecretKeySpec(data, algorithm);
//生成一个指定Mac算法的Mac对象
Mac mac = Mac.getInstance(algorithm);
//用给定密钥初始化Mac对象
mac.init(secretKey);
byte[] text = input.getBytes(StandardCharsets.UTF_8);
byte[] encryptByte = mac.doFinal(text);
//cipher = bytesToHexStr(encryptByte);
cipher = bytesToBase64(encryptByte);
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
e.printStackTrace();
}
return cipher;
}
/**
* byte数组转16进制字符串
*
* @param bytes byte数组
* @return hex字符串
*/
public static String bytesToHexStr(byte[] bytes) {
StringBuilder hexStr = new StringBuilder();
for (byte b : bytes) {
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
hexStr.append(hex);
}
return hexStr.toString();
}
//转成base64字符串
public static String bytesToBase64(byte[] bytes){
String base64encodedString = Base64.getEncoder().encodeToString(bytes);
return base64encodedString;
}
}