All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.envision.energy.util.SignUtil Maven / Gradle / Ivy

There is a newer version: 3.0.3
Show newest version
package com.envision.energy.util;

import java.util.Arrays;
import java.util.Map;

public class SignUtil {
	public static String sign(String appKey, String secret, Map paramMap)
    {
        // 拼接有序的参数名-值串
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(appKey);
        if(paramMap != null) {
	        // 对参数名进行字典排序
	        String[] keyArray = paramMap.keySet().toArray(new String[0]);
	        Arrays.sort(keyArray);
	        for (String key : keyArray)
	        {
	            stringBuilder.append(key).append(paramMap.get(key));
	        }
        }

        stringBuilder.append(secret);
        String codes = stringBuilder.toString();

        // SHA-1编码, 这里使用的是Apache
        // codec,即可获得签名(shaHex()会首先将中文转换为UTF8编码然后进行sha1计算,使用其他的工具包请注意UTF8编码转换)
        /*
         * 以下sha1签名代码效果等同 byte[] sha =
         * org.apache.commons.codec.digest.DigestUtils
         * .sha(org.apache.commons.codec
         * .binary.StringUtils.getBytesUtf8(codes)); String sign =
         * org.apache.commons
         * .codec.binary.Hex.encodeHexString(sha).toUpperCase();
         */
        String sign = org.apache.commons.codec.digest.DigestUtils.shaHex(codes).toUpperCase();

        return sign;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy