From 3b56809ffefc1fbdd84e76b634ee8846edf04169 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 14:51:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=82=AE=E7=AE=B1=E8=84=B1?= =?UTF-8?q?=E6=95=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define.go | 2 ++ execute.go | 4 +++- mail.go | 36 ++++++++++++++++++++++++++++++++++++ mail_test.go | 23 +++++++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 mail.go create mode 100644 mail_test.go diff --git a/define.go b/define.go index febd542..ac09382 100644 --- a/define.go +++ b/define.go @@ -10,4 +10,6 @@ package data_mask const ( // TypeDefault 默认实现 TypeDefault = "DEFAULT_DATA_MASK" + // TypeMail 邮箱数据脱敏 + TypeMail = "MAIL_DATA_MASK" ) diff --git a/execute.go b/execute.go index 9de32f5..3bb1443 100644 --- a/execute.go +++ b/execute.go @@ -13,7 +13,9 @@ var ( ) func init() { - maskInstanceList := []IDataMask{} + maskInstanceList := []IDataMask{ + &MailDataMask{}, // 邮箱脱敏 + } for _, item := range maskInstanceList { maskInstanceTable[item.Type()] = item } diff --git a/mail.go b/mail.go new file mode 100644 index 0000000..d0581b9 --- /dev/null +++ b/mail.go @@ -0,0 +1,36 @@ +// Package data_mask ... +// +// Description : data_mask ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-05-24 14:40 +package data_mask + +import "strings" + +// MailDataMask ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:40 2023/5/24 +type MailDataMask struct { +} + +func (m MailDataMask) Mask(input string) string { + input = strings.TrimSpace(input) + if !strings.Contains(input, "@") || strings.HasPrefix(input, "@") { + // 不是邮箱 + return input + } + arr := strings.Split(input, "@") + if len(arr) != 2 { + return input + } + + return string(arr[0][0]) + "********" + string(arr[0][len(arr[0])-1]) + "@" + arr[1] +} + +func (m MailDataMask) Type() string { + return TypeMail +} diff --git a/mail_test.go b/mail_test.go new file mode 100644 index 0000000..2dc609b --- /dev/null +++ b/mail_test.go @@ -0,0 +1,23 @@ +// Package data_mask ... +// +// Description : data_mask ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-05-24 14:47 +package data_mask + +import ( + "fmt" + "testing" +) + +// TestMailDataMask_Mask ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:47 2023/5/24 +func TestMailDataMask_Mask(t *testing.T) { + i := &MailDataMask{} + fmt.Println(i.Mask("baichaqinghuan123@123.com")) +}