JAVA 接口签名sign生成 工具类

import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

/**
 * @Author: JCccc
 * @CreateTime: 2018-10-30
 * @Description:
 */
public class SignUtils {



    public String getTimestamp() {
        //生成时间戳

        long timestampLong =System.currentTimeMillis();

        String timestampStr = String.valueOf(timestampLong);

        return timestampStr;
    }



    public String getNonceStr(int length){

        //生成随机字符串

        String str="zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";

        Random random=new Random();

        StringBuffer randomStr=new StringBuffer();

        // 设置生成字符串的长度,用于循环

        for(int i=0; i params, String privateKey){
        StringBuilder sb = new StringBuilder();
        // 将参数以参数名的字典升序排序
        Map sortParams = new TreeMap(params);
        // 遍历排序的字典,并拼接"key=value"格式
        for (Map.Entry entry : sortParams.entrySet()) {
            String key = entry.getKey();
            String value =  entry.getValue().trim();
            if (!StringUtils.isEmpty(value))
                sb.append("&").append(key).append("=").append(value);
        }
        String stringA = sb.toString().replaceFirst("&","");
       String privateKey="WHJBK24NXCX"; //私钥最后放在配置文件里面读取
        String stringSignTemp = stringA + "&"+"appkey="+privateKey;
        //将签名使用MD5加密并全部字母变为大写
         String signValue = Md5Encrypt.md5(stringSignTemp).toUpperCase();
         System.out.println("stringA+privateKey后MD5加密+转换全部大写生成sign为:       "+signValue);
        return signValue;
    }


}

你可能感兴趣的:(Java,Java工具类)