Class DataMasker

java.lang.Object
com.oorian.security.DataMasker

public class DataMasker extends Object
Utility class for masking sensitive data (PII) in display and log output.

DataMasker provides methods to partially redact personally identifiable information such as Social Security Numbers, credit card numbers, email addresses, and phone numbers. Each method preserves enough of the original value for identification while hiding the sensitive portions.

Usage:


 DataMasker.maskSsn("123-45-6789")       // "***-**-6789"
 DataMasker.maskCreditCard("4111111111111111") // "************1111"
 DataMasker.maskEmail("user@example.com") // "u***@example.com"
 DataMasker.maskPhone("(555) 867-5309")   // "(***) ***-5309"
 
Since:
2.1
Version:
1.0
Author:
Marvin P. Warble Jr.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final char
    The default masking character.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    mask(String value, int revealSuffix)
    Masks a generic string, revealing only the specified number of trailing characters.
    static String
    mask(String value, int revealPrefix, int revealSuffix)
    Masks a generic string, revealing only the specified number of leading and trailing characters.
    static String
    maskCreditCard(String cardNumber)
    Masks a credit card number, revealing only the last four digits.
    static String
    Masks an email address, hiding most of the local part while preserving the first character and the full domain.
    static String
    Masks a phone number, revealing only the last four digits.
    static String
    Masks a Social Security Number, revealing only the last four digits.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MASK_CHAR

      public static final char MASK_CHAR
      The default masking character.
      See Also:
  • Method Details

    • maskSsn

      public static String maskSsn(String ssn)
      Masks a Social Security Number, revealing only the last four digits.

      Accepts SSNs with or without dashes. Returns the masked value in ***-**-NNNN format.

      Parameters:
      ssn - the SSN to mask
      Returns:
      the masked SSN, or null if the input is null
    • maskCreditCard

      public static String maskCreditCard(String cardNumber)
      Masks a credit card number, revealing only the last four digits.

      All digits except the last four are replaced with the mask character. Non-digit characters (spaces, dashes) are preserved.

      Parameters:
      cardNumber - the credit card number to mask
      Returns:
      the masked card number, or null if the input is null
    • maskEmail

      public static String maskEmail(String email)
      Masks an email address, hiding most of the local part while preserving the first character and the full domain.

      Example: "john.doe@example.com" becomes "j***@example.com"

      Parameters:
      email - the email address to mask
      Returns:
      the masked email, or null if the input is null
    • maskPhone

      public static String maskPhone(String phone)
      Masks a phone number, revealing only the last four digits.

      Non-digit characters (parentheses, spaces, dashes) in the original formatting are replaced with the mask character, except the last four digits and their surrounding formatting are preserved.

      Parameters:
      phone - the phone number to mask
      Returns:
      the masked phone number, or null if the input is null
    • mask

      public static String mask(String value, int revealSuffix)
      Masks a generic string, revealing only the specified number of trailing characters.

      Useful for masking account numbers, API keys, tokens, and other sensitive identifiers.

      Parameters:
      value - the string to mask
      revealSuffix - the number of trailing characters to reveal
      Returns:
      the masked string, or null if the input is null
    • mask

      public static String mask(String value, int revealPrefix, int revealSuffix)
      Masks a generic string, revealing only the specified number of leading and trailing characters.
      Parameters:
      value - the string to mask
      revealPrefix - the number of leading characters to reveal
      revealSuffix - the number of trailing characters to reveal
      Returns:
      the masked string, or null if the input is null