MD5SignUtil

package net.joystart.common.util;

import java.security.MessageDigest;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import sun.misc.BASE64Encoder;

public class MD5SignUtil {
	private Map<String, String> map = new HashMap<String, String>();

	/**
	 * @Description: 签名构造函数
	 * @author FFCS
	 * @param <header>:iCtrlCmdId sUserKey sVin lTime 
	 * @param <body>:oCtrl(data)
	 * @param  privateKey(云度私下提供)
	 * @time 2017/10/12
	 * */
	public MD5SignUtil(String iCtrlCmdId, String sUserKey, String sVin, String lTime, Map<String, String> oCtrl,String privateKey) {
		Map<String, String> mapShort = new HashMap<String, String>();
		mapShort.put("iCtrlCmdId", iCtrlCmdId);
		mapShort.put("sUserKey", sUserKey);
		mapShort.put("sVin", sVin);
		mapShort.put("lTime", lTime);
		mapShort.put("privateKey",privateKey);
		mapShort.putAll(oCtrl);
		map=sortMapByKey(mapShort); //使用 Map按key进行排序
	}

	/**
	 * @Description: 获取签名
	 * @author FFCS
	 * @param 
	 * @time 2017/10/12
	 * */
	public String sign() throws Exception {
		String s = "";
		for (Map.Entry<String, String> entry : map.entrySet()) {
			s += entry.getKey();
			s += "=";
			s +=entry.getValue();
        }
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		BASE64Encoder base64 = new BASE64Encoder();
		String sign = base64.encode(md5.digest(s.getBytes("utf-8")));
		return sign;
	}
	
	/**
     * 使用 Map按key进行排序
     * @param map
     * @return
     */
    public static Map<String, String> sortMapByKey(Map<String, String> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        Map<String, String> sortMap = new TreeMap<String, String>(new MapKeyComparator());

        sortMap.putAll(map);

        return sortMap;
    }
 
}

//比较器类
class MapKeyComparator implements Comparator<String>{
	 @Override
	 public int compare(String str1, String str2) {
	        return str1.compareTo(str2);
	   }
}

 

转载于:https://www.cnblogs.com/cuijinlong/p/8867321.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值