diff --git a/define.go b/define.go index ac09382..9b53558 100644 --- a/define.go +++ b/define.go @@ -12,4 +12,6 @@ const ( TypeDefault = "DEFAULT_DATA_MASK" // TypeMail 邮箱数据脱敏 TypeMail = "MAIL_DATA_MASK" + // TypePhone 手机号脱敏 + TypePhone = "PHONE_DATA_MASK" ) diff --git a/execute.go b/execute.go index 3bb1443..e8c23aa 100644 --- a/execute.go +++ b/execute.go @@ -14,7 +14,8 @@ var ( func init() { maskInstanceList := []IDataMask{ - &MailDataMask{}, // 邮箱脱敏 + &MailDataMask{}, // 邮箱脱敏 + &PhoneDataMask{}, // 手机号脱敏 } for _, item := range maskInstanceList { maskInstanceTable[item.Type()] = item diff --git a/mail_test.go b/mail_test.go index 2dc609b..8e3e1f2 100644 --- a/mail_test.go +++ b/mail_test.go @@ -19,5 +19,6 @@ import ( // Date : 14:47 2023/5/24 func TestMailDataMask_Mask(t *testing.T) { i := &MailDataMask{} - fmt.Println(i.Mask("baichaqinghuan123@123.com")) + input := "baichaqinghuan123@123.com" + fmt.Println(input, "--------------->", i.Mask(input)) } diff --git a/phone.go b/phone.go new file mode 100644 index 0000000..25ba097 --- /dev/null +++ b/phone.go @@ -0,0 +1,38 @@ +// Package data_mask ... +// +// Description : data_mask ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-05-24 14:52 +package data_mask + +import "regexp" + +// PhoneDataMask ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:57 2023/5/24 +type PhoneDataMask struct { +} + +func (p PhoneDataMask) Mask(input string) string { + // 匹配规则 + // ^1第一位为一 + // [345789]{1} 后接一位345789 的数字 + // \\d \d的转义 表示数字 {9} 接9位 + // $ 结束符 + regRuler := "^1[345789]{1}\\d{9}$" + // 正则调用规则 + reg := regexp.MustCompile(regRuler) + // 返回 MatchString 是否匹配 + if !reg.MatchString(input) { + return input + } + return string(input[0:3]) + "******" + string(input[9:11]) +} + +func (p PhoneDataMask) Type() string { + return TypePhone +} diff --git a/phone_test.go b/phone_test.go new file mode 100644 index 0000000..a5c3bf0 --- /dev/null +++ b/phone_test.go @@ -0,0 +1,19 @@ +// Package data_mask ... +// +// Description : data_mask ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-05-24 14:57 +package data_mask + +import ( + "fmt" + "testing" +) + +func TestPhoneDataMask_Mask(t *testing.T) { + i := &PhoneDataMask{} + input := "18940952935" + fmt.Println(input, "------------>", i.Mask(input)) +}