1 Star 0 Fork 0

wosylf/龙飞工具仓库

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
数据脱敏.go 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
longfei 提交于 2024-07-16 14:22 +08:00 . 优化一下结构
package xutil
import (
"regexp"
"strings"
)
func F判断是否为脱敏手机号(str string) (b bool) {
return !strings.Contains(str, "****")
}
// 手机号,身份证号,邮件脱敏
func F数据脱敏(str string) (result string) {
if str == "" {
return "***"
}
if strings.Contains(str, "@") { //邮箱
res := strings.Split(str, "@")
if len(res[0]) < 3 {
resString := "***"
result = resString + "@" + res[1]
} else {
res2 := Substr2(str, 0, 3)
resString := res2 + "***"
result = resString + "@" + res[1]
}
return result
} else {
reg := `^1[0-9]\d{9}$`
rgx := regexp.MustCompile(reg)
mobileMatch := rgx.MatchString(str)
if mobileMatch {
result = Substr2(str, 0, 3) + "****" + Substr2(str, 7, 11)
} else {
nameRune := []rune(str)
lens := len(nameRune)
if lens <= 1 {
result = "***"
} else if lens == 2 {
result = string(nameRune[:1]) + "*"
} else if lens == 3 {
result = string(nameRune[:1]) + "*" + string(nameRune[2:3])
} else if lens == 4 {
result = string(nameRune[:1]) + "**" + string(nameRune[lens-1:lens])
} else if lens > 4 {
result = string(nameRune[:2]) + "***" + string(nameRune[lens-2:lens])
}
}
return
}
}
func Substr2(str string, start int, end int) string {
rs := []rune(str)
return string(rs[start:end])
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wosylf/ltool.git
git@gitee.com:wosylf/ltool.git
wosylf
ltool
龙飞工具仓库
4aab2f4b94f9

搜索帮助