参数升幂排序,MD5

Scroll Down

MD5用的支付宝的包

  //排序str , str + 密钥,   进行MD5
    private static String sort(Map<String, Object> map) {
        boolean empty = map.isEmpty();
        if (empty == false) {
            String sortStr = buildSignStr(map);
            String key = "&key=";
            String recordStr = sortStr+key;
            String sign = MD5.sign(recordStr, KEY, "UTF-8");
            LOGGER.info("签名后加密串为=========>>>> " +  sign);
            return sign;
        } else {
            LOGGER.error("签名入参map为空,请自行校验参数后再试");
            return "";
        }
    }

    /**
     * TreeMap
     * 参数名 ASCII 码从小到大排序(字典序)
     * @param params
     * @return
     */
    public static final String buildSignStr(Map<String, Object> params) {
        StringBuilder sb = new StringBuilder();
        // 将参数以参数名的字典升序排序
        Map<String, Object> sortParams = new TreeMap<String, Object>(params);
        // 遍历排序的字典,并拼接"key=value"格式
        for (Map.Entry<String, Object> entry : sortParams.entrySet()) {
            if (sb.length() != 0) {
                sb.append("&");
            }
            sb.append(entry.getKey()).append("=").append(entry.getValue());
        }
        System.err.println(sb.toString());
        return sb.toString();
    }