package com.bonus.common.houqin.utils;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.PrimitiveArrayUtil;
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 com.bonus.common.houqin.constant.GlobalConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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(GlobalConstants.CANTEEN_YIZHAN));
      System.out.println(sm4Decrypt("I7p6PJ37nNv9ZogUY0YPBQ==##encrypted"));

      String src = decryptBySm4("ucOf6NO83HW94e6LBThhN02qGaWMomk5FYpu/9mRB6rG5fIj4juCZHFYg9Z0rFFQ",
              Base64Decoder.decode("StqtdHhleMKClUGbmBqdrg=="));
      System.out.println(src);
      String src1 = decryptBySm4("43AzgBDxXiusgEUakhEQTE3XQ2CNbHsSVU8mcn23t200QvzbNCIndRujOPNFZwHSyG3UhvnN6S6YMWVhEFRYAhvnjFODl9szbTcQOFX8WakXB+QTRLFCrVMTc0B01FXLlChFGqRxouG5/mbeleDf+BSkeSFjhpgPrJoXjOlgP9jo2tS+IlYT+dVjsqBsAihQGUAsSyU1gXIFwbl+Wp/bVbnt8w34WXXX/oFJq5Nkpe2xr9APu7wG90jnVSm7bcHTLY1PrZFyg52Dha6dB6c6H+Jc31HA4hCMoRSfDsMlopI4JyHWnQCRIRJvPD5VDLQ+PhBgw7wobk43lA6/FZU8U1zvm6SpxcwI3014sxQTBWfZ1JWCogZvh06Ou0KNlsAEuAnzpCf9sby2ji2z5jifqTHew2GTTcp+gOz0vIMushdhf2mk8Ifw5NzCaTNdsmYdAHDpx5xmTgZS8fG8thBoXMaPuz9DvnAET6wST2/KNEVsDredtOvRV+Mlid44ibE2+z/ajx62RgMRipOBhpkAjNTT9iUUrUqpaA8dSeK2JyYBTYea/fQ4uXjYTjZER18JTimV2PtQO/X73s5NVOZAWXME9wfzHWVCKyxAg6IZnZky1rJ+d/HIKIV4LmT5J36NUnu2JlLP6AuUAaZa1BrbuwBhQO+P4du/NdhT5zZlU1sRCmcNteKr3/cF7e2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CtyJKZofM65Fhrbl4Lx0qAcq21cBsBmfw9xjFmDcxkF0PSSxPg4sFVCiUQb+GzT54CXYACtZoWSBuHC3gwEousc6E7fnNZwEFbttwe+eN+/wmTR8uQrSPQ5t9wF+OMoYt1QDnGHt4LR5+q1BWzWjITMsnlA1TOBYmuT1OX8quh3TggnGCv1WUjqIqcaZ7PzRHGS9GJUeTmUzwNg7zrPQ++B4V1uVxv16fnSnnkvfb/HfoDYZyNrOaAuNrNOh0/cBUrqn6EvtSZRC5LlHriXTtEuUmlL0+rRgigxfZeyi8oicYqDCxg6WJRYYa+sankFQxe+DqCNJ2B39n0k5HF6VVJdnvZzLsF2K4rBvxpKQl4/H/gO4B9NIh6Hb83pznp5pSYHoQ4YnKtvX",
              Base64Decoder.decode(src));
      System.out.println(src1);
   }

   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);
      }
   }
}
