http使用权限签名算法
http使用权限签名算法
创建SignCheck.java签名算法文件
package com.crs.ticket.utils; import java.io.UnsupportedEncodingException; import java.security.SignatureException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.crs.ticket.utils.httpclient.HttpProtocolHandler; import com.crs.ticket.utils.httpclient.HttpRequest; import com.crs.ticket.utils.httpclient.HttpResponse; import com.crs.ticket.utils.httpclient.HttpResultType; /** * 功能:MD5签名处理核心文件,不需要修改 * 版本:1.0 * 修改日期:2014-09-19 * 说明: * */ public class SignCheck { private static Logger logger = LoggerFactory.getLogger(SignCheck.class); /** * @param content * @param charset * @return * @throws SignatureException * @throws UnsupportedEncodingException */ private static byte[] getContentBytes(String content, String charset) { if (charset == null || "".equals(charset)) { return content.getBytes(); } try { return content.getBytes(charset); } catch (UnsupportedEncodingException e) { throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset); } } /** * 除去数组中的空值和签名参数 * @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("sign_type")) { continue; } result.put(key, value); } return result; } 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; } /** * MAP类型数组转换成NameValuePair类型 * * @param properties * MAP类型数组 * @return NameValuePair类型数组 */ private static NameValuePair[] generatNameValuePair( Map<String, String> properties) { NameValuePair[] nameValuePair = new NameValuePair[properties.size()]; int i = 0; for (Map.Entry<String, String> entry : properties.entrySet()) { nameValuePair[i++] = new NameValuePair(entry.getKey(), entry.getValue()); } return nameValuePair; } //默认的加密编码 public static String DEFAULT_INPUT_CHARSET = "UTF-8"; /** /** * 根据传入参数获取签名字符串 * @param params * @return */ public static String getSignByMap(Map<String,String> params,String key){ //过滤空值、sign与sign_type参数 Map<String, String> sParaNew = paraFilter(params); //获取待签名字符串 String text = createLinkString(sParaNew); //获取签名字符集 String input_charset = "UTF-8"; text = text + key; String mysign = DigestUtils.md5Hex(getContentBytes(text, input_charset)); return mysign; } }
划船不用桨、杨帆不等风、一生全靠浪