1

I want to mask the email in this format xxx####xx##@x####.x##. x -> to display the character , # -> to hide the character.

For example:

  • input: testemail@gmail.com,
  • output: tes##ma##@g####.c##

This is the code I have written

private static String maskEmailId(String email, String format) {
    int start = format.indexOf("#");
    int end = email.indexOf("@");
    String result = maskPattern(start, end, email);

    start = email.indexOf('@')+ (format.indexOf("#", format.indexOf("@")) - format.indexOf("@") - 1);
    end = email.indexOf('.');
    result = maskPattern(start + 1, end, result);

    start = email.indexOf(".") + (format.indexOf("#", format.indexOf(".")) - format.indexOf(".") - 1);
    end = email.length();
    result = maskPattern(start + 1, end, result);

    return result;
}

private static String maskPattern(int start, int end, String email) {
    StringBuilder sb = new StringBuilder(email);
    for (int i = start; i < end; i++) {
        sb.setCharAt(i, '#');
    }
    return sb.toString();
}

I am bit confused with the logic if I want to display and hide in between after hiding first time.

Can anyone help on this?

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
sandeep
  • 11
  • 1
  • Look at these https://stackoverflow.com/questions/16775253/how-masking-of-sensitive-data-is-achieved-using-slf4j-framework/61360391#61360391 https://stackoverflow.com/questions/25277930/mask-sensitive-data-in-logs-with-logback/61360967#61360967 – Alexandr Kovalenko Dec 27 '21 at 07:42
  • Thanks @Alexandr Kovalenko – sandeep Dec 30 '21 at 12:08
  • I have seen the documents but I have not got the solution masking and displaying the characters in the email dynamically , because the email length varies for different user. If I get any documents on it would be helpful. – sandeep Dec 30 '21 at 12:38

0 Answers0