package com.palacesun.masking.tools.sm4fpe;

import java.util.Map;

public class SM4FPECellPhoneUtil {
   public static String generateKey() {
      SM4FPEBase base = new SM4FPEBase();
      return base.generateKey();
   }

   public static String encryptEcb(String hexKey, String a, String isLike, String keepfirst, String encdigit, String twoindex) throws Exception {
      Map<String, String> map = SM4FPETools.parseInput(a, keepfirst, encdigit, twoindex);
      String prefix = (String)map.get("prefix");
      String suffix = (String)map.get("suffix");
      String middle = (String)map.get("middle");
      String out = middle;
      if (SM4FPECellPhone.check(middle)) {
         SM4FPECellPhone phone = new SM4FPECellPhone();
         out = phone.encryptEcb(hexKey, middle);
         if (!SM4FPECellPhone.check(out)) {
            System.out.println("The encrypt result must be phone. result:" + out);
            out = middle;
         }
      }

      return prefix + out + suffix;
   }

   public static String decryptEcb(String hexKey, String input, String isLike, String keepfirst, String encdigit, String twoindex) throws Exception {
      Map<String, String> map = SM4FPETools.parseInput(input, keepfirst, encdigit, twoindex);
      String prefix = (String)map.get("prefix");
      String suffix = (String)map.get("suffix");
      String middle = (String)map.get("middle");
      String out = middle;
      if (SM4FPECellPhone.check(middle)) {
         SM4FPECellPhone phone = new SM4FPECellPhone();
         out = phone.decryptEcb(hexKey, middle);
         if (!SM4FPECellPhone.check(out)) {
            System.out.println("The encrypt result must be phone. result:" + out);
            out = middle;
         }
      }

      return prefix + out + suffix;
   }
}
