package com.sqx.modules.pay.wuyou;

import com.sqx.modules.utils.MD5Util;

import java.util.HashMap;
import java.util.Map;

import static com.sqx.modules.pay.wuyou.Constants.APP_SECRET;

/**
 * 无忧支付  签名加密
 * @author GYJ
 */
public class Encrypt {


    public static String getParamsSign(Map<String, Object> params) {
        // 将参数按照key=value的格式拼接在一起， key按照字典序排序
        StringBuilder sb = new StringBuilder();
        params.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(entry -> {
            sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        });
        sb.append("key=").append(APP_SECRET);
        String signStr = sb.toString();
        System.out.println("signStr: " + signStr);
        return MD5Util.encodeByMD5(signStr).toUpperCase();
    }

    public static void main(String[] args) {
        Map<String, Object> params = new HashMap<>();
        params.put("a", "1");
        params.put("b", "2");
        String sign = getParamsSign(params);
        System.out.println(sign);
    }
}
