簽名算法java
package net.wit.util; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; import javax.servlet.http.HttpServletRequest; import org.apache.commons.codec.digest.DigestUtils; /** * 類MD5Sign.java的實現描述:MD5簽名和驗籤 * * */ public class MD5Sign { /** * 方法描述:將字符串MD5加碼 生成32位md5碼 * * [@author](https://my.oschina.net/arthor) leon 2016年10月10日 下午3:02:30 * [@param](https://my.oschina.net/u/2303379) inStr * [@return](https://my.oschina.net/u/556800) */ public static String md5(String inStr) { try { return DigestUtils.md5Hex(inStr.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new RuntimeException("MD5簽名過程當中出現錯誤"); } } /** * 方法描述:簽名字符串 * * * [@param](https://my.oschina.net/u/2303379) params 須要簽名的參數 * [@param](https://my.oschina.net/u/2303379) appSecret 簽名密鑰 * @return */ public static String sign(HashMap<String, String> params, String appSecret) { StringBuilder valueSb = new StringBuilder(); params.put("appSecret", appSecret); // 將參數以參數名的字典升序排序 Map<String, String> sortParams = new TreeMap<String, String>(params); Set<Entry<String, String>> entrys = sortParams.entrySet(); // 遍歷排序的字典,並拼接value1+value2......格式 for (Entry<String, String> entry : entrys) { valueSb.append(entry.getValue()); } params.remove("appSecret"); return md5(valueSb.toString()); } /** * 方法描述:驗證簽名 * * * @param appSecret 加密祕鑰 * @param request * @return * @throws Exception */ public static boolean verify(String appSecret, HttpServletRequest request) throws Exception { String sign = request.getParameter("sign"); if (sign == null) { throw new Exception(URLDecoder.decode("請求中沒有帶簽名","UTF-8")); } if (request.getParameter("timestamp") == null) { throw new Exception(URLDecoder.decode("請求中沒有帶時間戳","UTF-8")); } Long timestamp=Long.parseLong(request.getParameter("timestamp")); Long second = (System.currentTimeMillis() - timestamp) / (1000 * 60); if(second>10){ throw new Exception(URLDecoder.decode("timestamp有效期超過十分鐘","UTF-8")); } HashMap<String, String> params = new HashMap<String, String>(); // 獲取url參數 @SuppressWarnings("unchecked") Enumeration<String> enu = request.getParameterNames(); while (enu.hasMoreElements()) { String paramName = enu.nextElement().trim(); if (!paramName.equals("sign")) { // 拼接參數值字符串並進行utf-8解碼,防止中文亂碼產生 params.put(paramName, URLDecoder.decode(request.getParameter(paramName), "UTF-8")); } } //params.put("appSecret", appSecret); // 將參數以參數名的字典升序排序 Map<String, String> sortParams = new TreeMap<String, String>(params); Set<Entry<String, String>> entrys = sortParams.entrySet(); // 遍歷排序的字典,並拼接value1+value2......格式 StringBuilder valueSb = new StringBuilder(); for (Entry<String, String> entry : entrys) { valueSb.append(entry.getValue()); } String mysign = md5(md5(valueSb.toString().toUpperCase().toString()+appSecret)).toUpperCase().toString(); if (mysign.equals(sign)) { return true; } else { throw new Exception(URLDecoder.decode("簽名不正確","UTF-8")); } } }