72 lines
1.7 KiB
Go
72 lines
1.7 KiB
Go
// Package data_mask ...
|
|
//
|
|
// Description : data_mask ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2023-05-23 14:25
|
|
package data_mask
|
|
|
|
import (
|
|
"errors"
|
|
"git.zhangdeman.cn/zhangdeman/data_mask/strategy"
|
|
)
|
|
|
|
var (
|
|
maskInstanceTable = make(map[string]IDataMask)
|
|
defaultDataMask = &DefaultDataMask{}
|
|
)
|
|
|
|
func init() {
|
|
maskInstanceList := []IDataMask{
|
|
&strategy.MailDataMask{}, // 邮箱脱敏
|
|
&strategy.PhoneDataMask{}, // 手机号脱敏
|
|
&strategy.PasswordDataMask{}, // 密码脱敏
|
|
&DefaultDataMask{}, // 默认脱敏
|
|
&strategy.NameDataMask{}, // 姓名脱敏
|
|
}
|
|
for _, item := range maskInstanceList {
|
|
maskInstanceTable[item.Type()] = item
|
|
}
|
|
}
|
|
|
|
// RegisterStrategy 注册数据脱敏策略
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 22:18 2024/4/7
|
|
func RegisterStrategy(dataMaskStrategy ...IDataMask) {
|
|
for _, item := range dataMaskStrategy {
|
|
maskInstanceTable[item.Type()] = item
|
|
}
|
|
}
|
|
|
|
// Execute 执行数据脱敏
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 14:31 2023/5/23
|
|
func Execute(data string, maskType string) string {
|
|
dataMaskInstance, exist := maskInstanceTable[maskType]
|
|
if !exist || nil == dataMaskInstance {
|
|
dataMaskInstance = defaultDataMask
|
|
}
|
|
return dataMaskInstance.Mask(data)
|
|
}
|
|
|
|
// ExecuteWithError 严格执行
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 22:33 2024/4/7
|
|
func ExecuteWithError(data string, maskType string) (string, error) {
|
|
dataMaskInstance, exist := maskInstanceTable[maskType]
|
|
if !exist {
|
|
return "", errors.New(maskType + " : data mask type is not found")
|
|
}
|
|
if nil == dataMaskInstance {
|
|
return "", errors.New(maskType + " : data mask type instance is nil")
|
|
}
|
|
return Execute(data, maskType), nil
|
|
}
|