package com.ruoyi.demo.utils;

import cn.hutool.core.map.MapUtil;
import cn.hutool.crypto.SecureUtil;

import java.util.Map;

public class SignUtil {

    public static String sign(Map<String, Object> params, String xmlData, String appSecret) {
        params.remove("sign");
        String data = MapUtil.sortJoin(params, "", "", true, xmlData);
        data = appSecret + data + appSecret;
        String md5 = SecureUtil.md5(data);
        return md5.toUpperCase();
    }

    public static boolean verify(Map<String, Object> params, String xmlData, String appSecret) {
        String signed = (String) params.remove("sign");
        String sign = sign(params, xmlData, appSecret);
        return sign.equals(signed);
    }

    public static String sign(Map<String, Object> params, String appSecret) {
        params.remove("sign");
        String data = MapUtil.sortJoin(params, "", "", true);
        data = appSecret + data + appSecret;
        String md5 = SecureUtil.md5(data);
        return md5.toUpperCase();
    }

    public static boolean verify(Map<String, Object> params, String appSecret) {
        String signed = (String) params.remove("sign");
        String sign = sign(params, appSecret);
        System.out.println("signed:"+signed);
        System.out.println("sign:"+sign);
        return sign.equals(signed);
    }
}
