package com.basic.security.utils; import android.util.Base64; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; public class Base64Util { public static Map> tableIgnoreKeys = new HashMap<>(); public static boolean init = false; public static void init() { if (init) { return; } init = true; tableIgnoreKeys.put("person", Arrays.asList( "id", "del_flag", "table", "device_id", "company_id", "all_device", "update_time", "sign_up_time", "phone", "id_card_number", "checked_time_rule_id", "camera_image_feature_path", "camera_image_path", "camera_image_fastdfs_path", "id_card_image_path", "auto_init" )); tableIgnoreKeys.put("base_setting", Arrays.asList( "auto_register_rule", "auto_checked_time_rule_id", "indoor_device_id", "relation_person", "relation_device", "indoor_device_ip", "no_face_delay", "picture_rotation_angle", "id", "auto_mode", "admin_mode", "pass_mode", "visitor_mode", "admin_normal_verify", "admin_normal_verify_phone", "admin_normal_verify_idcard", "admin_normal_verify_name", "admin_idcard_verify", "admin_idcard_verify_phone", "back_to_home", "image_play_interval", "detect_multiple_face" , "face_ratio", "face_angle", "image_play_time", "login_expire_long", "auto_mode_verify_phone", "auto_normal_verify", "auto_normal_verify_phone", "auto_normal_verify_idcard", "auto_normal_verify_name", "auto_idcard_verify", "auto_idcard_verify_phone", "auto_register_identity_id", "device_id", "company_id", "table", "del_flag" )); tableIgnoreKeys.put("business", Arrays.asList( "id", "create_time", "only_allow", "only_allow_start", "only_allow_end", "chose_from_time_rule", "checked_time_rule_id", "access_table_name", "access_identity", "access_name", "not_reached_time_remind", "confirm_pass_identity", "confirm_pass_name", "time_ids", "device_id", "company_id", "table", "del_flag" )); tableIgnoreKeys.put("business_apply_device", Arrays.asList( "id", "business_id", "device_id", "table", "del_flag" )); tableIgnoreKeys.put("business_person", Arrays.asList( "id", "person_id", "business_id", "del_flag", "table" )); tableIgnoreKeys.put("device", Arrays.asList( "id", "ip", "device_id", "mask", "open_door_time", "company_id", "del_flag", "table" )); tableIgnoreKeys.put("identity", Arrays.asList( "id", "device_id", "company_id", "del_flag", "table" )); tableIgnoreKeys.put("person_identity", Arrays.asList( "id", "person_id", "identity_id", "del_flag", "device_id", "company_id", "table" )); tableIgnoreKeys.put("temporary_person", Arrays.asList( "ip", "table", "save_time", "camera_image_feature", "camera_image_path" )); tableIgnoreKeys.put("time", Arrays.asList( "id", "week_json", "date_json", "is_use", "del_flag", "table" )); tableIgnoreKeys.put("visit", Arrays.asList( "id", "person_id", "visit_time", "verify_result", "exit_time", "target_person_id", "target_org_id", "person_id_number", "person_phone", "device_id", "company_id", "del_flag", "table" )); tableIgnoreKeys.put("org", Arrays.asList()); List tableList = new ArrayList<>(); for (String table : tableIgnoreKeys.keySet()) { tableList.add(table); } // BaseManager.syncTableNames = tableList; } public static String encodeToString(String table, String key, String strValue) { try { if (needConvert(table, key)) { strValue = Base64.encodeToString(strValue.getBytes("UTF-8"), Base64.NO_WRAP); strValue = "base64_" + strValue; } } catch (UnsupportedEncodingException e) { System1.out.println(e.getMessage()); } return strValue; } private static boolean needConvert(String table, String key) { if (1 == 1) { return false; } if (key != null) { if (key.startsWith("\"")) { } if (key.endsWith("\"")) { key = key.substring(0, key.length() - 1); } if (key.startsWith("`")) { } if (key.endsWith("`")) { key = key.substring(0, key.length() - 1); } List ignoreKeys = tableIgnoreKeys.get(table); return ignoreKeys == null || !ignoreKeys.contains(key); } return false; } public static String decodeToString(String table, String key, String strValue) { try { if (strValue != null) { if (needConvert(table, key) || strValue.startsWith("base64_")) { if (strValue.startsWith("base64_")) { strValue = strValue.substring("base64_".length()); strValue = new String(Base64.decode(strValue, Base64.NO_WRAP), "UTF8"); } } } } catch (Exception e) { // e.printStackTrace(); System1.out.println("Base64Util.decodeToString execption=" + e.getMessage()); } return strValue; } public static String featureBytes2Base64(byte[] bytes) { try { if (bytes != null) { if (bytes.length == 2560) { // System1.out.println("encode feature="+feature); return Base64.encodeToString(bytes, Base64.NO_WRAP); } else { // System1.out.println("feature长度不对(encode) " + FrameUtil.getFrames()); } } } catch (Exception e) { e.printStackTrace(); } return null; } public static byte[] featureBase642Bytes(String strValue) { try { if (strValue != null && strValue.length() > 50) { byte[] bytes = Base64.decode(strValue, Base64.NO_WRAP); if (bytes != null) { if (bytes.length == 2560) { return bytes; } else { System1.out.println("feature长度不对(decode)"); } } } } catch (Exception e) { e.printStackTrace(); } return null; } }