Class OpenMask
Features | 主要功能:
- Mobile phone masking - 手机号脱敏
- ID card masking - 身份证号脱敏
- Email masking - 邮箱脱敏
- Bank card masking - 银行卡号脱敏
- Chinese name masking - 中文姓名脱敏
- Custom pattern masking - 自定义模式脱敏
Usage Examples | 使用示例:
String phone = OpenMask.mobile("13812345678"); // "138****5678"
String email = OpenMask.email("test@example.com"); // "t***t@example.com"
String name = OpenMask.chineseName("张三丰"); // "张**"
String card = OpenMask.bankCard("6222021234567890"); // "6222****7890"
Security | 安全性:
- Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-string V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic Stringstatic StringchineseName(String name) static Stringdesensitize(String value, DesensitizeType type) static Stringstatic Stringstatic Stringstatic StringmaskAddress(String address) static StringmaskBankCard(String cardNo) static StringmaskByPattern(String str, String pattern, char maskChar) Masks all occurrences of a regex pattern in a string.static Stringstatic StringMasks the middle portion of a string with a fixed-length placeholder, regardless of the original input length.static StringmaskIdCard(String idCard) static StringmaskMiddle(String str, int keepLen, char maskChar) static StringmaskMobile(String mobile) static Stringstatic String
-
Method Details
-
mobile
-
maskMobile
-
idCard
-
maskIdCard
-
email
-
maskEmail
-
bankCard
-
maskBankCard
-
chineseName
-
maskName
-
maskAddress
-
mask
-
maskMiddle
-
maskFixed
Masks the middle portion of a string with a fixed-length placeholder, regardless of the original input length. 用固定长度占位符掩码字符串中间部分,与原输入长度无关。Unlike
mask(String, int, int, char)where the mask grows with the input length (and therefore reveals it), this method always emits the exactfixedMaskstring for the middle. Useful for sensitive credentials such as device tokens, API keys, and OAuth tokens, where leaking the original length is itself a security concern. 与mask(String, int, int, char)不同 — 后者掩码长度随输入增长(从而泄露长度), 本方法始终输出固定的fixedMask。适用于设备令牌、API Key、OAuth Token 等敏感凭据,避免长度泄露作为侧信道。If the input is too short (
length <= startKeep + endKeep), the entirefixedMaskis returned instead of the original string — because returning a short input verbatim is itself a leak. 当输入过短(length <= startKeep + endKeep)时返回fixedMask, 而非原字符串 — 直接返回短输入本身就是泄露。Examples | 示例:
OpenMask.maskFixed("abcdefghijklmnop", 4, 4, "***"); // "abcd***mnop" OpenMask.maskFixed("abcdefghijklmnopqrst", 4, 4, "***"); // "abcd***qrst" (length 20, still 3 chars) OpenMask.maskFixed("short", 4, 4, "***"); // "***" OpenMask.maskFixed(null, 4, 4, "***"); // null- Parameters:
str- the string to mask | 要掩码的字符串startKeep- visible prefix length, must be>= 0| 保留前缀长度,必须>= 0endKeep- visible suffix length, must be>= 0| 保留后缀长度,必须>= 0fixedMask- fixed-length mask placeholder,nulltreated as empty | 固定长度掩码占位符,null视为空串- Returns:
- masked string,
fixedMaskif input is too short, ornullif input is null | 掩码后的字符串;输入过短时返回fixedMask;输入为 null 时返回 null - Throws:
IllegalArgumentException- ifstartKeeporendKeepis negative | 当startKeep或endKeep为负数时抛出- Since:
- opencode-base-string V1.0.4
-
maskByPattern
Masks all occurrences of a regex pattern in a string. 使用正则表达式模式掩码字符串中所有匹配项。Security | 安全性: The
patternparameter is compiled as a regular expression. To prevent ReDoS, patterns longer than 200 characters are rejected. The mask replacement is treated as a literal string (not a regex replacement), so backreferences such as$1inmaskCharhave no effect. 参数pattern被编译为正则表达式。为防止 ReDoS,长度超过 200 字符的模式会被拒绝。 掩码替换视为字面量(非正则替换),maskChar中的反向引用(如$1)不生效。- Parameters:
str- the string to mask | 要掩码的字符串pattern- the regex pattern to match | 要匹配的正则表达式模式maskChar- the character to replace each match | 替换每个匹配的字符- Returns:
- the masked string, or
nullif input is null | 掩码后的字符串,如果输入为null则返回null - Throws:
IllegalArgumentException- if pattern exceeds 200 characters | 如果模式超过200字符
-
desensitize
-