package com.supwisdom.util;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
/**
* HmacSHA1加密工具类
*/
public class HMACUtil {
/**
* 生成签名
* @param data
* @param key
* @return
*/
public static String HMACSHA1(String data, String key) {
byte[] byteHMAC = null;
try {
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec spec = new SecretKeySpec(key.getBytes(), "HmacSHA1");
mac.init(spec);
byteHMAC = mac.doFinal(data.getBytes("UTF-8"));
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException ignore) {
ignore.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (byteHMAC != null) {
try {
String hexMac = getHexString(byteHMAC);
return hexMac;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
return null;
}
private static String getHexString(byte[] b) throws Exception {
String result = "";
for (int i = 0; i < b.length; i++) {
result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
}
return result;
}
public static void main(String[] args){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmms");
String format = sdf.format(new java.util.Date());
String s="partner_id=100008&sign_method=HMAC&stuempno=2017000782×tamp="+format+"";
String key="6c86d9aad103499e9e7148e33af080af";
System.out.println(HMACSHA1(s, key)+"**"+format);
}
}
StringUtils
package com.supwisdom.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 字符串工具类
*
* @author LEE
*/
public class StringUtils {
/**
* 除去数组中的空值和签名参数
* @param sArray 签名参数组
* @return 去掉空值与签名参数后的新签名参数组
*/
public static Map<String, String> paramFilter(Map<String, String> sArray) {
Map<String, String> result = new HashMap<String, String>();
if (sArray == null || sArray.size() <= 0) {
return result;
}
for (String key : sArray.keySet()) {
String value = sArray.get(key);
if (value == null || value.equals("") || key.equalsIgnoreCase("sign")) {
continue;
}
result.put(key, value);
}
return result;
}
/**
* 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
* @param params 需要排序并参与字符拼接的参数组
* @return 拼接后字符串
*/
public static String createLinkString(Map<String, String> params) {
List<String> keys = new ArrayList<String>(params.keySet());
Collections.sort(keys);
String prestr = "";
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
String value = params.get(key);
if (i == keys.size() - 1) {//拼接时,不包括最后一个&字符
prestr = prestr + key + "=" + value;
} else {
prestr = prestr + key + "=" + value + "&";
}
}
return prestr;
}
/**
* 检查是否为空
*
* @param s
* @return
*/
public static boolean isEmpty(String s) {
if (null == s || s.trim().length() == 0) {
return true;
} else {
return false;
}
}
/**
* 防止SQL注入
*
* @param str
* @return
*/
public static String transactSQLInjection(String str) {
return str.replaceAll(".*([';]+|(--)+).*", " ");
}
/**
* 计算含有中文字符的字符串长度
*
* @param sStr
* @return
*/
public static int getHzStringLen(String sStr) {
if (null == sStr) {
return 0;
}
int i = 0, j = 0;
String chinese = "[\u4e00-\u9fa5]";
try {
while (j < sStr.length()) {
String temp = sStr.substring(j, j + 1);
// 判断是否为中文字符
if (temp.matches(chinese)) {
i += 2;
} else
i += 1;
j = j + 1;
}
return i;
} catch (Exception e) {
return 0;
}
}
/**
* 右对齐,左侧增加空格
*
* @param sourceStr
* 字符串
* @param ilen
* 最终长度
* @return
*/
public static String leftBlankAppend(String sourceStr, int ilen) {
if (null == sourceStr) {
return "";
}
String res = sourceStr;
int i = getHzStringLen(res);// res.length();
int j = 0;
if (i >= ilen)
res = res.substring(0, ilen);
else {
for (j = 0; j < ilen - i; j++)
res = " " + res;
}
return res;
}
/**
* 右对齐,左侧增加0
*
* @param sourceStr
* 字符串
* @param ilen
* 最终长度
* @return
*/
public static String leftZeroAppend(String sourceStr, int ilen) {
if (null == sourceStr) {
return "";
}
String res = sourceStr;
int i = getHzStringLen(res);// res.length();
int j = 0;
if (i >= ilen)
res = res.substring(0, ilen);
else {
for (j = 0; j < ilen - i; j++)
res = "0" + res;
}
return res;
}
/**
* 右侧增加空格
*
* @param sourceStr
* 字符串
* @param ilen
* 最终长度
* @return
*/
public static String rightBlankAppend(String sourceStr, int ilen) {
if (null == sourceStr) {
sourceStr = "";
}
String res = sourceStr;
int i = getHzStringLen(res);// res.length();
int j = 0;
if (i >= ilen)
res = res.substring(0, ilen);
else {
for (j = 0; j < ilen - i; j++)
res = res + " ";
}
return res;
}
/**
* 将byte数组转为16进制字符串
*
* @param src
* @return
*/
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv.toUpperCase());
}
return stringBuilder.toString();
}
/**
* Convert hex string to byte[]
*
* @param hexString
* the hex string
* @return byte[]
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
/**
* char转为byte
* @param c
* @return
*/
public static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
}