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 +}