From 7f490196537815ddaf75df1e60500be8c7dab0f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 23 Sep 2024 16:30:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A7=93=E5=90=8D=E8=84=B1=E6=95=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/strategy_type.go | 14 ++++++------- execute.go | 7 +++++-- strategy/name.go | 44 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 strategy/name.go diff --git a/define/strategy_type.go b/define/strategy_type.go index 09e58f6..f334ebf 100644 --- a/define/strategy_type.go +++ b/define/strategy_type.go @@ -8,14 +8,11 @@ package define const ( - // TypeDefault 默认实现 - TypeDefault = "DEFAULT_DATA_MASK" - // TypeMail 邮箱数据脱敏 - TypeMail = "MAIL_DATA_MASK" - // TypePhone 手机号脱敏 - TypePhone = "PHONE_DATA_MASK" - // TypePassword 密码数据脱敏 - TypePassword = "PASSWORD_DATA_MASK" + TypeDefault = "DEFAULT_DATA_MASK" // 默认实现 + TypeMail = "MAIL_DATA_MASK" // 邮箱数据脱敏 + TypePhone = "PHONE_DATA_MASK" // 手机号脱敏 + TypePassword = "PASSWORD_DATA_MASK" // 密码数据脱敏 + TypeName = "NAME_DATA_MASK" // 姓名数据脱敏 ) // TypeDesc 脱敏类型描述 @@ -34,5 +31,6 @@ var ( {TypeMail, "邮箱数据脱敏"}, {TypePhone, "手机号数据脱敏"}, {TypePassword, "密码数据脱敏"}, + {TypeName, "姓名数据脱敏"}, } ) diff --git a/execute.go b/execute.go index 5639356..202033a 100644 --- a/execute.go +++ b/execute.go @@ -19,8 +19,11 @@ var ( func init() { maskInstanceList := []IDataMask{ - &strategy.MailDataMask{}, // 邮箱脱敏 - &strategy.PhoneDataMask{}, // 手机号脱敏 + &strategy.MailDataMask{}, // 邮箱脱敏 + &strategy.PhoneDataMask{}, // 手机号脱敏 + &strategy.PasswordDataMask{}, // 密码脱敏 + &DefaultDataMask{}, // 默认脱敏 + &strategy.NameDataMask{}, // 姓名脱敏 } for _, item := range maskInstanceList { maskInstanceTable[item.Type()] = item diff --git a/strategy/name.go b/strategy/name.go new file mode 100644 index 0000000..08d6d1f --- /dev/null +++ b/strategy/name.go @@ -0,0 +1,44 @@ +// Package strategy ... +// +// Description : strategy ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-09-23 16:25 +package strategy + +import ( + "git.zhangdeman.cn/zhangdeman/data_mask/define" + "strings" +) + +// NameDataMask 姓名脱敏 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:30 2024/9/23 +type NameDataMask struct { +} + +func (n *NameDataMask) Mask(input string) string { + if len(input) == 0 { + return "" + } + nameArr := strings.Split(input, "") + if len(nameArr) == 2 { + return nameArr[0] + "*" + } + return nameArr[0] + n.getMaskString(len(nameArr)) + nameArr[len(nameArr)-1] +} + +func (n *NameDataMask) Type() string { + return define.TypeName +} + +func (n *NameDataMask) getMaskString(nameLength int) string { + maskStr := "" + for i := 0; i < nameLength-2; i++ { + maskStr = maskStr + "*" + } + return maskStr +}