package com.bonus.common.core.utils.encryption;

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.SM4;
import com.bonus.common.core.utils.StringUtils;

/**
 * sm4 加解密 工具类
 * @author 黑子
 */
public class Sm4Utils {
   private static  String S1="3b893ea109d729ba7eb1a841649b5e6d";
    private static String S2="2cc0c5f9f1749f1632efa9f63e9013cd";
    /**
     * 解密
     * @param encodeText
     * @return
     */
    public  static  String decode(String encodeText){
        try{
            if(StringUtils.isEmpty(encodeText)){
                return "";
            }
            SM4 sm4 = new SM4(Mode.CBC, Padding.PKCS5Padding, HexUtil.decodeHex(S1),HexUtil.decodeHex(S2));
            return sm4.decryptStr(encodeText);
        }catch (Exception e){
            return  encodeText;
        }
    }
    /**
     * 加密
     * @param encodeText
     * @return
     */
    public  static  String encode(String encodeText){
        try{
            if(StringUtils.isEmpty(encodeText)){
                return "";
            }
            SM4 sm4 = new SM4(Mode.CBC, Padding.PKCS5Padding, HexUtil.decodeHex(S1),HexUtil.decodeHex(S2));
            return sm4.encryptHex(encodeText);
        }catch (Exception e){
            return  encodeText;
        }
    }

}
