From 984657032d5aa35fa604e3d3af0a5d9b7b305d15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 24 May 2023 15:00:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=89=8B=E6=9C=BA=E5=8F=B7?= =?UTF-8?q?=E8=84=B1=E6=95=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define.go | 2 ++ execute.go | 3 ++- mail_test.go | 3 ++- phone.go | 38 ++++++++++++++++++++++++++++++++++++++ phone_test.go | 19 +++++++++++++++++++ 5 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 phone.go create mode 100644 phone_test.go 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)) +}