public class ZCloudSignUtil { public static boolean verify(Map<String, String> params, String md5_key) { if (params.get("sign") == null) { return false; } String sign = params.get("sign"); // 过滤空值、sign与sign_type参数 Map<String, String> sParaNew = ZCloudCore.paraFilter(params); // 获取待签名字符串 String preSignStr = ZCloudCore.createLinkString(sParaNew); // 获得签名验证结果 return ZCloudMD5.verify(preSignStr, sign, md5_key); } public static String getSign(Map<String, String> params, String md5_key){ // 过滤空值、sign与sign_type参数 Map<String, String> sParaNew = ZCloudCore.paraFilter(params); // 获取待签名字符串 String preSignStr = ZCloudCore.createLinkString(sParaNew); return ZCloudMD5.sign(preSignStr, md5_key); } }
package net.joystart.baojia.sign; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 签名核心类 * @author 刘礼涛 * */ public class ZCloudCore { /** * 除去数组中的空值和签名参数 * @param sArray 签名参数组 * @return 去掉空值与签名参数后的新签名参数组 */ public static Map<String, String> paraFilter(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") || key.equalsIgnoreCase("msg")) { 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; } }
package net.joystart.baojia.sign; import java.io.UnsupportedEncodingException; import java.security.SignatureException; import org.apache.commons.codec.digest.DigestUtils; /** * 签名MD5工具类 * @author 刘礼涛 * */ public class ZCloudMD5 { /** * 签名字符串 * @param text 需要签名的字符串 * @param key 密钥 * @param input_charset 编码格式 * @return 签名结果 */ public static String sign(String text, String key) { text = text + key; return DigestUtils.md5Hex(getContentBytes(text)); } /** * 签名字符串 * @param text 需要签名的字符串 * @param sign 签名结果 * @param key 密钥 * @param input_charset 编码格式 * @return 签名结果 */ public static boolean verify(String text, String sign, String key) { text = text + key; String mysign = DigestUtils.md5Hex(getContentBytes(text)); if(mysign.equals(sign)) { return true; } else { return false; } } /** * @param content * @param charset * @return * @throws SignatureException * @throws UnsupportedEncodingException */ private static byte[] getContentBytes(String content) { try { return content.getBytes(ZCloudSignConfig.charset); } catch (UnsupportedEncodingException e) { throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,"); } } }