java md5簽名

簽名算法java

  • 將全部請求參數(key,value 爲一組),對數據結構按照 key 的升序,從新排序, 須要對 null 值進行過濾,須要將 boolean 型進行轉換爲 1 和 0
  • 將排序後參數組合爲新的字符串(keyvaluekeyvalue 中間無分割符)
  • 將生成好的字符串進行 MD5 加密
  • 將新的字符串,統一爲大寫字符串
  • 將生成好的字符串加上「私鑰」,造成新的字符串(私鑰爲 ONex-OMS 系統裏面生成)
  • 再進行一次 MD5 加密,獲得加密後的字符串
  • 再統一爲大寫字符串,獲得最終的簽名字符串 注意:從「系統級參數」開始到「業務級參數」所有做爲請求參數.sign 能夠不算入,若是 放進去,開始 sign = null ,也會被過濾掉的
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"));
        }

    }

}
相關文章
相關標籤/搜索