sensitive/readme.md

48 lines
1.2 KiB
Markdown
Raw Normal View History

2022-08-26 10:50:38 +08:00
## 脱敏 Desensitization
2022-08-26 10:35:14 +08:00
2022-08-26 10:50:38 +08:00
### 作用 Effect
2022-08-26 10:35:14 +08:00
利用反射处理结构体,处理带`sen`标签的字段(规则: `规则名称,占位符`,如:`phone,*`),将其脱敏。支持自定义处理函数
2022-08-26 10:50:38 +08:00
Use reflection to process the structure, process the fields with the `sen` tag (rule: `rule name, placeholder`, such as: `phone,*`), and desensitize it. Support custom handlers
### 定义结构体示例 Example
2022-08-26 10:35:14 +08:00
```go
package main
import (
"github.com/lixh00/sensitive"
)
type User struct {
Name string `json:"name"`
Age int `json:"age"`
Phone string `json:"phone" sen:"phone,*"`
2022-08-26 10:50:38 +08:00
Email string `json:"email" sen:"email,*"`
2022-08-26 10:35:14 +08:00
}
data := User{
Name: "lixh",
Age: 18,
Phone: "13888888888",
2022-08-26 10:50:38 +08:00
Email: "lixh@gmail.com",
2022-08-26 10:35:14 +08:00
}
2022-08-26 10:50:38 +08:00
// 添加自定义处理函数
// Add custom handler
sensitive.AddHandler("email", func(src, p string) string {
// 将@符号后面的替换为*
// Replace the after @ sign with *
idx := strings.Index(src, "@")
dst := src[:idx+1] + strings.Repeat(p, utf8.RuneCountInString(src)-idx-1)
return dst
})
2022-08-26 10:35:14 +08:00
if err := sensitive.Desensitize(data); err != nil {
fmt.Println(err)
}
bs, _ := json.Marshal(response)
2022-08-26 10:50:38 +08:00
log.Printf("after processing data: %v", string(bs))
2022-08-26 10:35:14 +08:00
```