package cn.hanghui.outapi.platform.common.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

/**
 * @author shisl
 * @package cn.hanghui.outapi.platform.common.utils
 * @class Sm4Utils
 * @date 2024/1/17 下午4:57
 * @description
 */
@Slf4j
public class Sm4Utils {

    /**
     * 加密
     *
     * @param privateKey 管理员提供
     * @param str
     * @return
     */
    public static String encrypt(final String privateKey, final String str) {
        if (StrUtil.isBlank((CharSequence) str)) {
            return "";
        }
        SymmetricCrypto sm4 = new SymmetricCrypto("SM4/ECB/PKCS5Padding", privateKey.getBytes());
        String sm4Encrypt = sm4.encryptHex(str, Charset.forName("UTF-8"));
        return sm4Encrypt;
    }

    /**
     * 解密
     *
     * @param privateKey 管理员提供
     * @param str
     * @return
     */
    public static String decrypt(final String privateKey, final String str) {
        if (StrUtil.isBlank((CharSequence) str)) {
            return null;
        }
        try {
            SymmetricCrypto sm4 = new SymmetricCrypto("SM4/ECB/PKCS5Padding", privateKey.getBytes());
            String sm4Decrypt = sm4.decryptStr(str, Charset.forName("UTF-8"));
            return sm4Decrypt;
        } catch (Exception e) {
            log.error("解密失败", e);
            return null;
        }
    }

    /**
     * 验证签名
     *
     * @param appKey
     * @param appSecret
     * @param sign
     * @param timestamp
     * @return
     */
    public static Boolean checkSign(String appKey, String appSecret, String sign, String timestamp) {
        if (Preconditions.isBlank(sign) || Preconditions.isBlank(timestamp)) {
            return Boolean.FALSE;
        }

        String dbSign = SecureUtil.md5(new StringBuilder().append(appKey)
                .append(appSecret)
                .append(timestamp).toString()).toLowerCase();
        if (!dbSign.equals(sign)) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    /**
     * 获取签名
     *
     * @param appKey
     * @param appSecret
     * @param timestamp
     * @return
     */
    public static String getSign(String appKey, String appSecret, String timestamp) {
        String sign = SecureUtil.md5(new StringBuilder().append(appKey)
                .append(appSecret)
                .append(timestamp).toString()).toLowerCase();
        return sign;
    }


    public static void main(String[] args) {
        //基础信息
        Map<String, Object> map = new HashMap<>();
        map.put("idNumber", "321323199007264912");
        map.put("name", "张亮");
        map.put("phone", "18762761616");
        map.put("sn", "136K008000700031");
//        RpRequestDTO data = new RpRequestDTO();
//        data.setJwh("330114003");
//        data.setLxdh("13173661471");
//        data.setPcs("派出所");
//        data.setSblx("1");
//        data.setSfzh("43122319880802261X");
//        data.setXm("郝炯淞");
//        data.setXzdxz("现住地详址");
//
//        Map<String, Object> map = new HashMap<>();
//        map.put("phone", new JSONObject().put("title","中文"));
//        map.put("name", "nnnnnnn");
        //58507aebe4053106f84e385ecd54464d
        String encrypt = Sm4Utils.encrypt("7a2bhucyhnvfsk9a", JSONObject.toJSONString(map));
        System.out.println(encrypt);
        String decrypt = Sm4Utils.decrypt("7a2bhucyhnvfsk9a", "6addedf78aa1decbd8d49be62df77dbe2daa473013ea9b9716c80d0513595c1cc7eb97d5405555c6a7f8a42d6648828f61deaf2627e0af80de4a343e82b9959b905d158398523c2c45530a1768f35bd4");
        System.out.println(decrypt);
        String timestamp = String.valueOf(System.currentTimeMillis());
        timestamp = "1709880389172";
        System.out.println(timestamp);
        System.out.println(Sm4Utils.getSign("hhsSehBkDLy", "aea2a0260ff50bd9f79dbbf0362165c363b885a1", timestamp));

    }


}
