http接口请求参数签名工具类的实现和测试代码

package com.yanek.test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
/**
* 请求参数签名工具类
*
*/
public class ParamsSignUtil {
	
	public static void main(String[] args)
	{
		
		//参数签名算法测试例子
		HashMap<String, String> signMap = new HashMap<String, String>();
		signMap.put("devid","920BF4CE-B9CC-449C-A146-BC5549D899ED");
		signMap.put("devtype","1");
		signMap.put("pcode","010210000");
		signMap.put("t","1472701601599");
		signMap.put("uid","0");
		signMap.put("v","4.5.3");
		signMap.put("ver","1.0");
		String secret_key="$3@%$$#"; //加密secret
		
		System.out.println("得到签名sign1:"+getSign(signMap,secret_key));
		HashMap<String, String> SignHashMap=ParamsSignUtil.sign(signMap, secret_key);
		//System.out.println("SignHashMap:"+SignHashMap);
	    String sign=(String)SignHashMap.get("appSign");
	    //获取参数签名字符串
		System.out.println("得到签名sign2:"+sign);



	
	}

	public static HashMap<String, String> sign(Map<String, String> paramValues,
			String secret) {
		return sign(paramValues, null, secret);
	}

	/**
	 * @param paramValues
	 * @param ignoreParamNames
	 * @param secret
	 * @return
	 */
	public static HashMap<String, String> sign(Map<String, String> paramValues,
			List<String> ignoreParamNames, String secret) {
		try {
			HashMap<String, String> signMap = new HashMap<String, String>();
			StringBuilder sb = new StringBuilder();
			List<String> paramNames = new ArrayList<String>(paramValues.size());
			paramNames.addAll(paramValues.keySet());
			if (ignoreParamNames != null && ignoreParamNames.size() > 0) {
				for (String ignoreParamName : ignoreParamNames) {
					paramNames.remove(ignoreParamName);
				}
			}
			Collections.sort(paramNames);
			sb.append(secret);
			for (String paramName : paramNames) {
				sb.append(paramName).append(paramValues.get(paramName));
			}
			sb.append(secret);
			byte[] md5Digest = getMD5Digest(sb.toString());
			String sign = byte2hex(md5Digest);
			signMap.put("appParam", sb.toString());
			signMap.put("appSign", sign);
			return signMap;
		} catch (IOException e) {
			throw new RuntimeException("加密签名计算错误", e);
		}
		
	}

	public static String utf8Encoding(String value, String sourceCharsetName) {
		try {
			return new String(value.getBytes(sourceCharsetName), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			throw new IllegalArgumentException(e);
		}
	}



	private static byte[] getMD5Digest(String data) throws IOException {
		byte[] bytes = null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			bytes = md.digest(data.getBytes("UTF-8"));
		} catch (GeneralSecurityException gse) {
			throw new IOException(gse);
		}
		return bytes;
	}


	private static String byte2hex(byte[] bytes) {
		StringBuilder sign = new StringBuilder();
		for (int i = 0; i < bytes.length; i++) {
			String hex = Integer.toHexString(bytes[i] & 0xFF);
			if (hex.length() == 1) {
				sign.append("0");
			}
			//sign.append(hex.toUpperCase());
			sign.append(hex.toLowerCase());
		}
		return sign.toString();
	}
	public static String getSign(Map<String, String> params,String secret)
	{
		String ret="";
		StringBuilder sb = new StringBuilder();
		Set<String> keyset=params.keySet();
		TreeSet<String> sortSet=new TreeSet<String>();
		sortSet.addAll(keyset);
		Iterator<String> it=sortSet.iterator();
		sb.append(secret);
		while(it.hasNext())
		{
			String key=it.next();
			String value=params.get(key);
			sb.append(key).append(value);
		}
		sb.append(secret);
		byte[] md5Digest;
		try {
			md5Digest = getMD5Digest(sb.toString());
			ret = byte2hex(md5Digest);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ret;
	}
	

}

输出如下:

得到签名sign1:d218314413dc847bc34ca8a10d7f9b1a
得到签名sign2:d218314413dc847bc34ca8a10d7f9b1a



©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值