//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.kingdee.eas.custom.szpgm.osf.task;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SignUtil {
    public static String generateSignature(Map<String, Object> params, String key) {
        if (params != null && !params.isEmpty()) {
            Map<String, String> sortedParamMap = new HashMap();

            for(Map.Entry<String, Object> entry : params.entrySet()) {
                String paramName = (String)entry.getKey();
                String paramValue = String.valueOf(entry.getValue());
                if (!"sign".equalsIgnoreCase(paramName) && paramValue != null && !paramValue.isEmpty()) {
                    String normalizedName = lowerFirstLetter(paramName);
                    sortedParamMap.put(normalizedName, paramValue);
                }
            }

            List<String> sortedKeys = new ArrayList(sortedParamMap.keySet());
            Collections.sort(sortedKeys);
            StringBuilder stringA = new StringBuilder();

            for(String keyName : sortedKeys) {
                if (stringA.length() > 0) {
                    stringA.append("&");
                }

                stringA.append(keyName).append("=").append((String)sortedParamMap.get(keyName));
            }

            String stringSignTemp = stringA.toString() + "&key=" + key;
            return md5Hex(stringSignTemp).toUpperCase();
        } else {
            return "";
        }
    }

    public static boolean verifySignature(Map<String, Object> params, String key, String sign) {
        if (params != null && sign != null) {
            String expectedSign = generateSignature(params, key);
            return expectedSign.equals(sign);
        } else {
            return false;
        }
    }

    private static String lowerFirstLetter(String name) {
        if (name != null && !name.isEmpty()) {
            char firstChar = name.charAt(0);
            if (Character.isUpperCase(firstChar)) {
                char[] chars = name.toCharArray();
                chars[0] = Character.toLowerCase(firstChar);
                return new String(chars);
            } else {
                return name;
            }
        } else {
            return name;
        }
    }

    private static String md5Hex(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(input.getBytes());
            StringBuilder sb = new StringBuilder();

            for(byte b : digest) {
                sb.append(String.format("%02x", b));
            }

            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 算法不支持", e);
        }
    }

//    public static void main(String[] args) {
//        String key = "C4C82EB86F63FE9CB2B71BA05DE06449";
//        Map<String, Object> params = new HashMap();
//        params.put("pageIndex", 1);
//        params.put("recordStartDate", "2026-05-27 00:00:00");
//        params.put("recordEndDate", "2026-05-27 23:59:59");
//        params.put("pageSize", 100);
//        params.put("timestamp", System.currentTimeMillis() / 1000L);
//        params.put("sign", "ORD123456");
//        params.put("empNo", "120176");
//        String sign = generateSignature(params, key);
//        System.out.println("生成的签名: " + sign);
//        System.out.println("生成的签名timestamp: " + System.currentTimeMillis() / 1000L);
//        params.put("sign", sign);
//        boolean valid = verifySignature(params, key, sign);
//        System.out.println("签名校验结果: " + valid);
//        System.out.println("签名校验结果: ");
//    }
}
