zoukankan      html  css  js  c++  java
  • 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);
    	   }
    }
    
  • 相关阅读:
    aws-lambda之异步实现文件的下载上传
    aws实例部署flask报错script-timed-out-before-returning-headers-application-py
    aws上部署scrapy,出现 Out of Memory,内存溢出
    在线UserAgent,爬虫UA
    ubuntu10.24 下安装 unixODBC coreseek4.1 手记
    ubuntu12.04 安装 python2.6
    coreseek/sphinx CentOS6.4下安装
    Elasticsearch 6.2.3 崩溃经历
    (转)梳理在线教育的几大金矿
    kangaroo-open 开源在线公开课平台
  • 原文地址:https://www.cnblogs.com/cuijinlong/p/8867321.html
Copyright © 2011-2022 走看看