package com.bonus.canteen.core.common.encrypt;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SM4;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.hutool.core.util.*;

public class SM4EncryptUtils {
   private static final Logger log = LoggerFactory.getLogger(SM4EncryptUtils.class);
   private static final DataEncDecProperties properties = getProperties();
   public static final String SUFFIX = "##encrypted";

   public static DataEncDecProperties getProperties() {
      DataEncDecProperties dataEncDecProperties = new DataEncDecProperties();
      dataEncDecProperties.setSm4Key("jY7bZz6Pjml+H/WZYfNSNA==");
      dataEncDecProperties.setEncrypted(true);
      dataEncDecProperties.setDesensitization(true);
      return dataEncDecProperties; //(DataEncDecProperties)SpringContextHolder.getBean(DataEncDecProperties.class);
   }

   public static String sm4Encrypt(String data) {
      try {
         return SmUtil.sm4(SecureUtil.decode(properties.getSm4Key())).encryptBase64(data) + "##encrypted";
      } catch (Exception var2) {
         log.debug("SM4加密异常:{}", var2.getMessage());
         return data;
      }
   }

   public static String sm4Encryptbyconfig(String data) {
      try {
         return properties.isEncrypted() ? SmUtil.sm4(SecureUtil.decode(properties.getSm4Key())).encryptBase64(data) + "##encrypted" : data;
      } catch (Exception var2) {
         log.debug("SM4加密异常:{}", var2.getMessage());
         return data;
      }
   }

   public static String desensitizedByConfig(String data, DesensitizedUtil.DesensitizedType desensitizedType) {
      try {
         if (properties.isDesensitization()) {
            return data.endsWith("##encrypted") ? DesensitizedUtil.desensitized(sm4Decrypt(data), desensitizedType) : DesensitizedUtil.desensitized(data, desensitizedType);
         } else {
            return data;
         }
      } catch (Exception var3) {
         log.debug("脱敏异常:{}", var3.getMessage());
         return data;
      }
   }

   public static String sm4Decrypt(String data) {
      try {
         if (data.endsWith("##encrypted")) {
            data = data.substring(0, data.length() - "##encrypted".length());
            return SmUtil.sm4(SecureUtil.decode(properties.getSm4Key())).decryptStr(data);
         } else {
            return data;
         }
      } catch (Exception var2) {
         log.debug("SM4解密异常:{}", var2.getMessage());
         return data;
      }
   }

   public static void main(String[] args) {
//      System.out.println(sm4Encrypt("18110922397"));
//      System.out.println(sm4Decrypt("J1KE3IyZqIqgWk09Z2gWpQ==##encrypted"));
//
//      System.out.println(sm4Encrypt("13866134935"));
//      System.out.println(sm4Decrypt("29G1AUHDmLvPe1tt0t12Dw==##encrypted"));

      String src = decryptBySm4("Ef9LKj+vPAWSlXh0ErbXxEkAcxQFnRF7FQg08WhFbMeyNaKQn8JC02ZImxZcJr1m",
              Base64Decoder.decode("StqtdHhleMKClUGbmBqdrg=="));
      System.out.println(src);
      String src1 = decryptBySm4("JddMNySsjnoQqRpdQyTYQlwitGKig8wWjXb6yKXOci9sPh0QSekRA/yukn0okWyCgSug31moaPURe1QJgzDO2/T1vsdqJbTmg/0vyAbmolNFYxkIY4NpviapEiBM2f0sHVj50oE5JCC0AgaZkSqx7YuWzbaZd+R3scGIlj/axLlCLz68FzKOpIVpNAaSs5o2DcoV4HW7FNvarXPuaVk314LQgz4ufmcwLsG/N/RGG9sfmYwhld9Jt2qpsfnY8rI+nOMw7XfpecV5gEHjjTVsPAfyX2YGSr1ECqrXiPKeVNklCdEaVA4HH1PwuOCPPv41FFjOzjaiJCHFqGHMTNsTGC3BMcSOFveFBTyuMqyKvg6LXtXTKa39vYj0Gu/g6wktL9vXTeDxHslSObe36Fw3X6tkbOU/xHawOihsHPl5qUVt/9LjB3USZJRBRt5uwgvhteY+SRtuBOjutpS6538SgUiGY5XkX8uFHu0MbJkxJSDVxb2hWsyRhaqDpeMH8m9kB9jBiBC6JIEcodJC+OMtzg==",
              Base64Decoder.decode(src));
      System.out.println(src1);

      String src2 = decryptBySm4("Fm61oDlfUUyw1827TmMJnMym9V7KeX90sFNBSEKklDCCF+xeP6xroJ8h51DSHEkLm9oLIz3zjZ67pyHQbNr1Ewy/IOa3oS3urOcs+GQM1Bbept49M4gYVlR+9T5PVBh/oGTxWCLDFGXniZ//I3TPzTZ4d0lvYYQXdU8urbRlG4h+HwVYHupezgESM/7znsxR1cgVRybPdAiabQBasPzJdI8VKx+g+xVR3ee2hPtLZTXBf6ncvbj4f6eXzq5Ak5NnDASJEdJC9gj4pv2evfogAxZNoFLS0hPRde44sjGBXpJ3pWLWp/03iIrLlCQC2V4qugUULZMWaRXp3VzXbvHxbjqzPybMsocBdylfpPDrlj8AMJwt4c48AV+5VSSX4OM7LWwiW7N1Wi8pXbzzzUiUPwJa2T5y3rd40RG8JCruBEoD58t68vt8PoCcdbi4oyO7iY0AZsGxqXkJML9khMzb7yyGogZ6UHVNuzEMZbwbCmMrr2TNIye47yWD4MrB4Em+AwoiCPQMOcZ4uoqtJ79z4+BKWnhb3wy9/Cre54gpLnnAPlEDcmsjVjphjebp9qeGQH9xYXgI/RujjjAUMVDfEIBCHehKjbxTNEMxKesxsQJ/oK4DTOJV71OK4FmxMzdIqkNj02nzoOJC1c7S6ybJcjSP0ls0KRkQq6BLlPxldzGkT6lIUcFfd9foqqETKVDhCisSmKK1oAkdqCd4G6DKF2HFnZmjlanss6KhjdNJoi5cPxpFfVWFG7WxnswOt8FH0eMbanwaY2OuLJ0IL/AoW89iEQQ1vOFCbA2ijrPvAZ8P/YVlXKxXKrcIBHTRfDNjbNrl4VF1/5b0PZWt2BO3BUbT1hSTW5T4M1K0XAqf91e11DgUcp0wlgXE9oyCrcWAMUqwCYYdWEMtvktN3gSeKCHrmHnb+BWHZDVwHIltTbz90guY0k7GgURXSHLPIu+O",
              Base64Decoder.decode(src));
      System.out.println(src2);


   }

   public static String decryptBySm4(String data, byte[] key) {
      byte[] decodedData = Base64Decoder.decode(data);
      if (decodedData.length <= 16) {
         throw new IllegalArgumentException("Encrypted data byte length must greater than 16");
      } else {
         byte[] iv = PrimitiveArrayUtil.sub(decodedData, 0, 16);
         byte[] dataBytes = PrimitiveArrayUtil.sub(decodedData, 16, decodedData.length);
         return (new SM4(Mode.CBC, Padding.PKCS5Padding, key, iv)).decryptStr(dataBytes);
      }
   }
}
