@Test public void testObfuscateAndShorten() { assertEquals("h***o", HoodUtil.obfuscateAndShorten("hello", 1, '*')); assertEquals("he***lo", HoodUtil.obfuscateAndShorten("hello", 2, '*')); assertEquals("he***lo", HoodUtil.obfuscateAndShorten("hello", 3, '*')); assertEquals("he***lo", HoodUtil.obfuscateAndShorten("hello", 4, '*')); assertEquals("mux***tre", HoodUtil.obfuscateAndShorten("muxadgztre", 3, '*')); assertEquals("h", HoodUtil.obfuscateAndShorten("h", 2, '*')); assertEquals("h***", HoodUtil.obfuscateAndShorten("he", 2, '*')); assertEquals("h***x", HoodUtil.obfuscateAndShorten("hex", 2, '*')); }
public static String obfuscateAndShorten(@Nullable String original, int maxCharLengthLeftOrRight, char obfuscationChar) { if (original == null || original.trim().length() <= 1) { return original; } if (maxCharLengthLeftOrRight <= 0) { throw new IllegalArgumentException("max char length must be gt 0"); } String star = String.valueOf(obfuscationChar); if (original.length() <= maxCharLengthLeftOrRight * 2) { return original.substring(0, original.length() / 2) + star + star + star + original.substring((original.length() / 2) + 1); } else { return original.substring(0, maxCharLengthLeftOrRight) + star + star + star + original.substring(original.length() - maxCharLengthLeftOrRight); } }
HoodUtil { public static String obfuscateAndShorten(@Nullable String original, int maxCharLengthLeftOrRight, char obfuscationChar) { if (original == null || original.trim().length() <= 1) { return original; } if (maxCharLengthLeftOrRight <= 0) { throw new IllegalArgumentException("max char length must be gt 0"); } String star = String.valueOf(obfuscationChar); if (original.length() <= maxCharLengthLeftOrRight * 2) { return original.substring(0, original.length() / 2) + star + star + star + original.substring((original.length() / 2) + 1); } else { return original.substring(0, maxCharLengthLeftOrRight) + star + star + star + original.substring(original.length() - maxCharLengthLeftOrRight); } } }
HoodUtil { public static String obfuscateAndShorten(@Nullable String original, int maxCharLengthLeftOrRight, char obfuscationChar) { if (original == null || original.trim().length() <= 1) { return original; } if (maxCharLengthLeftOrRight <= 0) { throw new IllegalArgumentException("max char length must be gt 0"); } String star = String.valueOf(obfuscationChar); if (original.length() <= maxCharLengthLeftOrRight * 2) { return original.substring(0, original.length() / 2) + star + star + star + original.substring((original.length() / 2) + 1); } else { return original.substring(0, maxCharLengthLeftOrRight) + star + star + star + original.substring(original.length() - maxCharLengthLeftOrRight); } } private  HoodUtil(); }
HoodUtil { public static String obfuscateAndShorten(@Nullable String original, int maxCharLengthLeftOrRight, char obfuscationChar) { if (original == null || original.trim().length() <= 1) { return original; } if (maxCharLengthLeftOrRight <= 0) { throw new IllegalArgumentException("max char length must be gt 0"); } String star = String.valueOf(obfuscationChar); if (original.length() <= maxCharLengthLeftOrRight * 2) { return original.substring(0, original.length() / 2) + star + star + star + original.substring((original.length() / 2) + 1); } else { return original.substring(0, maxCharLengthLeftOrRight) + star + star + star + original.substring(original.length() - maxCharLengthLeftOrRight); } } private  HoodUtil(); static String byteToHex(byte[] bytes); static T getConditionally(T object, boolean shouldReturnEntry); static Locale getCurrentLocale(Context ctx); static String humanReadableByteCount(long bytes, boolean si); static String millisToDaysHoursMinString(long millis); static String toSimpleDateTimeFormat(long millisEpochUtc); static String obfuscateAndShorten(@Nullable String original, int maxCharLengthLeftOrRight,
                                             char obfuscationChar); }
HoodUtil { public static String obfuscateAndShorten(@Nullable String original, int maxCharLengthLeftOrRight, char obfuscationChar) { if (original == null || original.trim().length() <= 1) { return original; } if (maxCharLengthLeftOrRight <= 0) { throw new IllegalArgumentException("max char length must be gt 0"); } String star = String.valueOf(obfuscationChar); if (original.length() <= maxCharLengthLeftOrRight * 2) { return original.substring(0, original.length() / 2) + star + star + star + original.substring((original.length() / 2) + 1); } else { return original.substring(0, maxCharLengthLeftOrRight) + star + star + star + original.substring(original.length() - maxCharLengthLeftOrRight); } } private  HoodUtil(); static String byteToHex(byte[] bytes); static T getConditionally(T object, boolean shouldReturnEntry); static Locale getCurrentLocale(Context ctx); static String humanReadableByteCount(long bytes, boolean si); static String millisToDaysHoursMinString(long millis); static String toSimpleDateTimeFormat(long millisEpochUtc); static String obfuscateAndShorten(@Nullable String original, int maxCharLengthLeftOrRight,
                                             char obfuscationChar);  }