first commit

This commit is contained in:
2024-07-23 10:23:43 +08:00
commit 7b4c2521a3
126 changed files with 15931 additions and 0 deletions

26
pkg/ddm/README.md Normal file
View File

@ -0,0 +1,26 @@
## DDM
动态数据掩码Dynamic Data Masking简称为DDM能够防止把敏感数据暴露给未经授权的用户。
| 类型 | 要求 | 示例 | 说明
| ---- | ---- | ---- | ----
| 手机号 | 前 3 后 4 | 132****7986 | 定长 11 位数字
| 邮箱地址 | 前 1 后 1 | l**w@gmail.com | 仅对 @ 之前的邮箱名称进行掩码
| 姓名 | 隐姓 | *鸿章 | 将姓氏隐藏
| 密码 | 不输出 | ****** |
| 银行卡卡号 | 前 6 后 4 | 622888******5676 | 银行卡卡号最多 19 位数字
| 身份证号 | 前 1 后 1 | 1******7 | 定长 18 位
#### 代码示例
```
// 返回值
type message struct {
Email ddm.Email `json:"email"`
}
msg := new(message)
msg.Email = ddm.Email("xinliangnote@163.com")
...
```

35
pkg/ddm/benchmark.go Normal file
View File

@ -0,0 +1,35 @@
package ddm
import (
"github.com/mritd/chinaid"
)
type BType uint8
const (
BMobile BType = iota
BIDNo
BName
BBankNo
BEmail
BAddress
)
func Benchmark(bType BType) string {
switch bType {
case BMobile:
return chinaid.Mobile()
case BIDNo:
return chinaid.IDNo()
case BEmail:
return chinaid.Email()
case BAddress:
return chinaid.Address()
case BName:
return chinaid.Name()
case BBankNo:
return chinaid.BankNo()
default:
return ""
}
}

62
pkg/ddm/mark.go Normal file
View File

@ -0,0 +1,62 @@
package ddm
import (
"fmt"
"strings"
)
func (m Mobile) MarshalJSON() ([]byte, error) {
if len(m) != 11 {
return []byte(`"` + m + `"`), nil
}
v := fmt.Sprintf("%s****%s", m[:3], m[len(m)-4:])
return []byte(`"` + v + `"`), nil
}
func (bc BankCard) MarshalJSON() ([]byte, error) {
if len(bc) > 19 || len(bc) < 16 {
return []byte(`"` + bc + `"`), nil
}
v := fmt.Sprintf("%s******%s", bc[:6], bc[len(bc)-4:])
return []byte(`"` + v + `"`), nil
}
func (card IDCard) MarshalJSON() ([]byte, error) {
if len(card) != 18 {
return []byte(`"` + card + `"`), nil
}
v := fmt.Sprintf("%s******%s", card[:1], card[len(card)-1:])
return []byte(`"` + v + `"`), nil
}
func (name IDName) MarshalJSON() ([]byte, error) {
if len(name) < 1 {
return []byte(`""`), nil
}
nameRune := []rune(name)
v := fmt.Sprintf("*%s", string(nameRune[1:]))
return []byte(`"` + v + `"`), nil
}
func (pw PassWord) MarshalJSON() ([]byte, error) {
v := "******"
return []byte(`"` + v + `"`), nil
}
func (e Email) MarshalJSON() ([]byte, error) {
if !strings.Contains(string(e), "@") {
return []byte(`"` + e + `"`), nil
}
split := strings.Split(string(e), "@")
if len(split[0]) < 1 || len(split[1]) < 1 {
return []byte(`"` + e + `"`), nil
}
v := fmt.Sprintf("%s***%s", split[0][:1], split[0][len(split[0])-1:])
return []byte(`"` + v + "@" + split[1] + `"`), nil
}

19
pkg/ddm/type.go Normal file
View File

@ -0,0 +1,19 @@
package ddm
// 手机号 132****7986
type Mobile string
// 银行卡号 622888******5676
type BankCard string
// 身份证号 1******7
type IDCard string
// 姓名 *鸿章
type IDName string
// 密码 ******
type PassWord string
// 邮箱 l***w@gmail.com
type Email string