mask
Masks an Aadhaar number for PII-safe display.
Aadhaar is strictly Private government PII (Aadhaar Act 2016 + DPDP Act 2023). Always mask before displaying in UI or writing to logs.
Masking operates on the raw input string (not normalised). This is intentional — the masker is a display utility working character-by-character on whatever it receives. Callers who want to mask a normalised Aadhaar should call format first: Aadhaar.mask(Aadhaar.format(value)).
Default (visibleStart=0, visibleEnd=4, maskChar='X'): Shows last 4 characters of the raw input, masks the rest. "234567890121" → "XXXXXXXX0121" (8 X's + last 4 digits)
Note on UIDAI-style spaced mask ("XXXX XXXX 0121"): The UIDAI standard display form is NOT a built-in output of mask(). To achieve it:
// Recommended — construct manually from raw digits
val raw = "234567890121"
val uidaiMask = "XXXX XXXX " + raw.takeLast(4) // -> "XXXX XXXX 0121"
// mask() on raw 12 digits gives "XXXXXXXX0121" (no spaces — char-by-char on raw)
// mask() on formatted 14-char string "2345 6789 0121" gives "XXXXXXXXXX0121"
// (masks spaces too — not the UIDAI form)This is a known limitation of the standard masking signature (consistent with OQ-10 contract). No additional API surface is added; compose the UIDAI form manually as shown above.
Edge cases (all safe — never throws):
Empty input → returns
"".visibleStart + visibleEnd >= value.length→ entire string returned unmasked (overlap rule).Non-normalised input → masking applied character-by-character on the raw string.
Return
Masked string. Never throws — display-safe by contract.
Parameters
Raw Aadhaar string to mask. No normalisation is applied.
Number of leading characters to show unmasked. Default: 0.
Number of trailing characters to show unmasked. Default: 4.
Character to replace masked positions. Default: 'X'.
Samples
check(Aadhaar.mask("234567890124") == "XXXXXXXX0124")
check(Aadhaar.mask("234567890124", visibleStart = 4, visibleEnd = 0) == "2345XXXXXXXX")