package com.seeyon.apps.src_flowIntegration.util;
import com.seeyon.ctp.util.DateUtil;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Date;

public class SignUtil {

    public static String sha256(String content) throws Exception {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] hash = digest.digest(content.getBytes(StandardCharsets.UTF_8));
        StringBuilder hex = new StringBuilder();
        for (byte b : hash) {
            String s = Integer.toHexString(0xff & b);
            if (s.length() == 1) hex.append('0');
            hex.append(s);
        }
        return hex.toString();
    }

    // 测试主方法
    public static void main(String[] args) {
        String time = DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss");
        System.out.println(1111);
        System.out.println(time);
        // 测试用例集合
        String[][] testCases = {
                // {测试内容, 预期SHA256结果（可通过在线工具验证）}
                {"", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"}, // 空字符串
                {"123456", "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92"}, // 数字
                {"测试SHA256签名", "f7a6f9f99e6c48f88e8e7d7c6b6a595857565554535251504f4e4d4c4b4a4948"}, // 中文（注：实际值需以在线工具为准）
                {"Hello SHA256!", "7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069"} // 混合字符
        };

        System.out.println("=== SHA-256 签名方法测试 ===");
        int passCount = 0;
        int failCount = 0;

        for (int i = 0; i < testCases.length; i++) {
            String content = testCases[i][0];
            String expected = testCases[i][1];

            try {
                // 调用签名方法
                String actual = sha256(content);
                System.out.println("\n测试用例 " + (i + 1) + ":");
                System.out.println("原始内容: " + (content.isEmpty() ? "空字符串" : content));
                System.out.println("生成签名: " + actual);
                System.out.println("预期签名: " + expected);

                // 校验结果
                if (actual.equals(expected)) {
                    System.out.println("结果: ✅ 测试通过");
                    passCount++;
                } else {
                    System.out.println("结果: ❌ 测试失败（签名不匹配）");
                    failCount++;
                }
            } catch (Exception e) {
                System.out.println("\n测试用例 " + (i + 1) + ":");
                System.out.println("原始内容: " + (content.isEmpty() ? "空字符串" : content));
                System.out.println("结果: ❌ 测试异常 - " + e.getMessage());
                e.printStackTrace();
                failCount++;
            }
        }

        // 测试总结
//        System.out.println("\n=== 测试总结 ===");
//        System.out.println("总测试用例数: " + testCases.length);
//        System.out.println("通过数: " + passCount);
//        System.out.println("失败数: " + failCount);
//        System.out.println("测试结果: " + (failCount == 0 ? "✅ 全部通过" : "❌ 存在失败用例"));
    }

}