实现说明
底层就是类 SecretMasker和函数 maskPan、maskEmail、main。
使用要点
- 执行文件,
main会按顺序走完maskPan、maskEmail。
常见问题
「日志字符串中的敏感值掩码」这个示例怎么运行?
运行这个文件就行;脚本没有任何外部要求。
入口签名:static String maskPan(String pan)
核心语句
return "*".repeat(pan.length() - 4) + pan.substring(pan.length() - 4);
return email.charAt(0) + "***" + email.substring(at);
return "*".repeat(pan.length());
代码示例
public class SecretMasker {
// Masking sensitive values: keep only the last four digits of a card number, star out the rest
static String maskPan(String pan) {
if (pan.length() < 4) {
return "*".repeat(pan.length());
}
return "*".repeat(pan.length() - 4) + pan.substring(pan.length() - 4);
}
// Emails keep the first character and the domain; the rest of the local part is masked
static String maskEmail(String email) {
int at = email.indexOf('@');
if (at <= 0) {
return "***";
}
return email.charAt(0) + "***" + email.substring(at);
}
public static void main(String[] args) {
System.out.println(maskPan("4111111111111111"));
System.out.println(maskPan("123"));
System.out.println(maskEmail("[email protected]"));
// Phone masking before log output: keep prefix and suffix, hide the middle four digits, and the log no longer carries full PII
String phone = "13800138000";
System.out.println(phone.substring(0, 3) + "****" + phone.substring(7));
}
}