package {{basePackage}}.utils.crypto;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.SecureRandom;

/**
 * dcrenl: 2023-09-08 16:15:49
 * 国密算法
 */
public class SmUtils {

    private static final SM2 sm2 = SmUtil.sm2();

    /**
     * dcrenl: 2023-09-11 9:5:9
     * sm4加密密钥，密钥长度16位
     */
    private static final String k = "ixUaL3m-uXV6qGlN";

    private static final SymmetricCrypto sm4 = SmUtil.sm4(k.getBytes(StandardCharsets.UTF_8));

    public static String sm2Encrypt(String str) {

        return sm2.encryptBase64(str, KeyType.PublicKey);
    }

    public static String sm2Decrypt(String str) {

        return sm2.decryptStr(str, KeyType.PrivateKey);
    }

    public static String sm2SignHex(String str) {

        return sm2.signHex(str);
    }

    public static boolean sm2VerifyHex(String str, String sign) {

        return sm2.verifyHex(str, sign);
    }


    public static String sm3DigestHex(String str) {

        return SmUtil.sm3(str);
    }

    /**
     * dcrenl: 2023-09-08 16:30:58
     * sm4加密
     * @param str
     * @return
     */
    public static String sm4Encrypt(String str) {

        return sm4.encryptBase64(str);
    }

    /**
     * dcrenl: 2023-09-08 16:31:16
     * sm4解密
     * @param str
     * @return
     */
    public static String sm4Decrypt(String str) {

        return sm4.decryptStr(str);
    }
}
