Class OpenMask

java.lang.Object
cloud.opencode.base.string.desensitize.OpenMask

public final class OpenMask extends Object
Data Masking Utility - Provides data desensitization and masking methods. 数据脱敏工具 - 提供数据脱敏和掩码方法。

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 Details

    • mobile

      public static String mobile(String mobile)
    • maskMobile

      public static String maskMobile(String mobile)
    • idCard

      public static String idCard(String idCard)
    • maskIdCard

      public static String maskIdCard(String idCard)
    • email

      public static String email(String email)
    • maskEmail

      public static String maskEmail(String email)
    • bankCard

      public static String bankCard(String cardNo)
    • maskBankCard

      public static String maskBankCard(String cardNo)
    • chineseName

      public static String chineseName(String name)
    • maskName

      public static String maskName(String name)
    • maskAddress

      public static String maskAddress(String address)
    • mask

      public static String mask(String str, int startKeep, int endKeep, char maskChar)
    • maskMiddle

      public static String maskMiddle(String str, int keepLen, char maskChar)
    • maskFixed

      public static String maskFixed(String str, int startKeep, int endKeep, String fixedMask)
      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 exact fixedMask string 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 entire fixedMask is 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 | 保留前缀长度,必须 >= 0
      endKeep - visible suffix length, must be >= 0 | 保留后缀长度,必须 >= 0
      fixedMask - fixed-length mask placeholder, null treated as empty | 固定长度掩码占位符,null 视为空串
      Returns:
      masked string, fixedMask if input is too short, or null if input is null | 掩码后的字符串;输入过短时返回 fixedMask;输入为 null 时返回 null
      Throws:
      IllegalArgumentException - if startKeep or endKeep is negative | 当 startKeependKeep 为负数时抛出
      Since:
      opencode-base-string V1.0.4
    • maskByPattern

      public static String maskByPattern(String str, String pattern, char maskChar)
      Masks all occurrences of a regex pattern in a string. 使用正则表达式模式掩码字符串中所有匹配项。

      Security | 安全性: The pattern parameter 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 $1 in maskChar have 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 null if input is null | 掩码后的字符串,如果输入为null则返回null
      Throws:
      IllegalArgumentException - if pattern exceeds 200 characters | 如果模式超过200字符
    • desensitize

      public static String desensitize(String value, DesensitizeType type)