package com.skr.mdm.util;

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.BCUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.SM2;
import lombok.extern.slf4j.Slf4j;

/**
 * @author nfq
 * @date 2020/12/11 11:17
 */
@Slf4j
public class SmUtils {

    private static final String PRIVATE_KEY = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgLtOt1wcOJEP97mCGm9WmC6OEnbWq1pGS4hhOWKA+e56gCgYIKoEcz1UBgi2hRANCAAT6nEP2aqwdJKtloMv7pMnAzDSOpvv1NizLNGsqK4wQkEc9t+P5/yWhy5fb3NQkoXJH69yWVKmjpqkkqkLwKEDq";
    private static final String PUBLIC_KEY = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAE+pxD9mqsHSSrZaDL+6TJwMw0jqb79TYsyzRrKiuMEJBHPbfj+f8locuX29zUJKFyR+vcllSpo6apJKpC8ChA6g==";

    public static final String PUSH_PRIVATE_KEY = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgNCdPG/yfw80pYYEI0fiFRa7r2lazNN2JEkUFq+FOIDugCgYIKoEcz1UBgi2hRANCAASwWHXVgW/ia+mLCzHt3jZKExv1MhPwbsSRojWPw8k4biPwWk9w7RjqOUCBBIBeIY2mAPcl2LZClfnq1xt7KKMD";
    public static final String PUSH_PUBLIC_KEY = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEsFh11YFv4mvpiwsx7d42ShMb9TIT8G7EkaI1j8PJOG4j8FpPcO0Y6jlAgQSAXiGNpgD3Jdi2QpX56tcbeyijAw==";

    //65字节 130
    private static final String CRYPT_PUB_KEY = "04abf60a03868fa04524c601c55c98122e7f987d14de380ea8a0802f1bd322ef6c8fc5ab05e54ffd8ee1b21970df5b3260d37922a6c14a73c1d202dbdc600c4893";
    //32字节 64
    private static final String CRYPT_PRI_KEY = "476eb7eefaa571fe8b7036b6840619df1d9f1e67a260ee0e17031829a99631a9";
    //65字节 130
    private static final String SIGN_PUB_KEY = "04ea77c0c06d82fb0e05eabbc0c1da1991d9ddbd7ec7c4bd8f8a19d3add726018e7c7a40248162b89d437ca2adea29ea7826db64bab161a6e071f831256f0f8639";
    //33字节 66
    private static final String SIGN_PRI_KEY = "00ad09198127dffd580f80a83c3daea23dadaa4ec910980304483c5edcf3be8aac";


    public static SM2 sm2() {
        return SmUtil.sm2(PRIVATE_KEY, PUBLIC_KEY);
    }

    public static SM2 sm2Crypt() {
        SM2 sm2 = SmUtil.sm2(BCUtil.toSm2Params(HexUtil.decodeHex(CRYPT_PRI_KEY)), BCUtil.toSm2Params(CRYPT_PUB_KEY.substring(2).substring(0, 64), CRYPT_PUB_KEY.substring(2).substring(64, 128)));
        sm2.usePlainEncoding();
        return sm2;
    }

    public static SM2 sm2Sign() {
        SM2 sm2Sign = SmUtil.sm2(BCUtil.toSm2Params(HexUtil.decodeHex(SIGN_PRI_KEY)), BCUtil.toSm2Params(SIGN_PUB_KEY.substring(2).substring(0, 64), SIGN_PUB_KEY.substring(2).substring(64, 128)));
        sm2Sign.usePlainEncoding();
        return sm2Sign;
    }

}
