/**
* 邮箱脱敏
*/
fun desensitizedEmail(email: String): String {
if (email.isNullOrEmpty() || email == "null") {
return "";
}
var index: Int = email.indexOf("@");
if(index == -1){
return email
}
var end: String = email.substring(index);
var sb: StringBuilder = StringBuilder();
sb.append(email.subSequence(0, 4));
for (i in 4 until index) {
sb.append("*");
}
sb.append(end);
return sb.toString();
}
/**
* 手机号码前三后四脱敏
*/
open fun mobileEncrypt(mobile: String): String? {
return if (mobile.isEmpty() || mobile.length != 11) {
mobile.replace("(\\w{3})\\w*(\\w{4})".toRegex(), "$1****$2")
} else mobile.replace("(\\w{3})\\w*(\\w{4})".toRegex(), "$1****$2")
}
/**
* 身份证号脱敏
*/
fun identityEncrypt(identity: String): String? {
var str = ""
if (identity.isEmpty()|| identity.length != 18) {
str = identity
} else {
var sign = ""
for (i in 0 until identity.length - 7) {
sign += "*"
}
str = identity.replace("(\\w{3})\\w*(\\w{4})".toRegex(), "$1${sign}$2")
}
/**
* 姓名脱敏
*/
fun desensitizedName(fullName: String): String? {
var name = ""
if (!fullName.isNullOrEmpty() && fullName.length > 1) {
name = fullName.substring(0, 1)
for (i in 1 until fullName.length) {
name += "*"
}
} else {
name = fullName
}
return name
}
Koltin 敏感信息脱敏
该代码段展示了几个用于处理敏感信息的函数,包括邮箱、手机号码、身份证号和全名的脱敏处理。这些函数使用正则表达式来保护用户的隐私数据,例如在邮箱中隐藏除前四位和后四位外的部分,在手机号码中隐藏中间四位,在身份证号中隐藏中间部分,在姓名中隐藏除首字母外的所有字符。
摘要由CSDN通过智能技术生成