Class DataMasker
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 -
Method Summary
Modifier and TypeMethodDescriptionstatic StringMasks a generic string, revealing only the specified number of trailing characters.static StringMasks a generic string, revealing only the specified number of leading and trailing characters.static StringmaskCreditCard(String cardNumber) Masks a credit card number, revealing only the last four digits.static StringMasks an email address, hiding most of the local part while preserving the first character and the full domain.static StringMasks a phone number, revealing only the last four digits.static StringMasks a Social Security Number, revealing only the last four digits.
-
Field Details
-
MASK_CHAR
public static final char MASK_CHARThe default masking character.- See Also:
-
-
Method Details
-
maskSsn
Masks a Social Security Number, revealing only the last four digits.Accepts SSNs with or without dashes. Returns the masked value in
***-**-NNNNformat.- Parameters:
ssn- the SSN to mask- Returns:
- the masked SSN, or
nullif the input is null
-
maskCreditCard
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
nullif the input is null
-
maskEmail
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
nullif the input is null
-
maskPhone
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
nullif the input is null
-
mask
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 maskrevealSuffix- the number of trailing characters to reveal- Returns:
- the masked string, or
nullif the input is null
-
mask
Masks a generic string, revealing only the specified number of leading and trailing characters.- Parameters:
value- the string to maskrevealPrefix- the number of leading characters to revealrevealSuffix- the number of trailing characters to reveal- Returns:
- the masked string, or
nullif the input is null
-