Some checks failed
Build / build (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
build / Build (push) Has been cancelled
GitHub Actions Mirror / mirror_to_gitee (push) Has been cancelled
GitHub Actions Mirror / mirror_to_gitlab (push) Has been cancelled
Issue Close Require / issue-close-require (push) Has been cancelled
Issue Check Inactive / issue-check-inactive (push) Has been cancelled
64 lines
1.8 KiB
Go
64 lines
1.8 KiB
Go
package utility
|
||
|
||
import "strings"
|
||
|
||
// DesensitizeGeneric 通用脱敏函数:
|
||
// startReserveCount: 从字符串开头保留的字符数
|
||
// endReserveCount: 从字符串末尾保留的字符数
|
||
// asteriskChar: 用于替换的字符 (例如 '*')
|
||
// 例如:DesensitizeGeneric("这是一个秘密信息", 2, 2, '*') -> 这**信息
|
||
func DesensitizeGeneric(text string, startReserveCount int, endReserveCount int, asteriskChar rune) string {
|
||
if text == "" {
|
||
return ""
|
||
}
|
||
runes := []rune(text)
|
||
length := len(runes)
|
||
|
||
// 计算实际需要保留的总字符数
|
||
totalReservedLength := startReserveCount + endReserveCount
|
||
|
||
// 如果需要保留的字符数大于等于总长度,则不脱敏
|
||
if totalReservedLength >= length {
|
||
return text
|
||
}
|
||
|
||
// 确保保留位数不为负数
|
||
if startReserveCount < 0 {
|
||
startReserveCount = 0
|
||
}
|
||
if endReserveCount < 0 {
|
||
endReserveCount = 0
|
||
}
|
||
|
||
// 如果保留总长度为0,且字符串不为空,则全部脱敏
|
||
if totalReservedLength == 0 && length > 0 {
|
||
return strings.Repeat(string(asteriskChar), length)
|
||
}
|
||
|
||
// 确保保留的长度不超过总长度
|
||
if startReserveCount > length {
|
||
startReserveCount = length
|
||
}
|
||
if endReserveCount > length-startReserveCount {
|
||
endReserveCount = length - startReserveCount
|
||
}
|
||
|
||
// 构造脱敏字符串
|
||
var sb strings.Builder
|
||
// 写入前缀
|
||
if startReserveCount > 0 {
|
||
sb.WriteString(string(runes[0:startReserveCount]))
|
||
}
|
||
// 写入星号
|
||
asteriskCount := length - startReserveCount - endReserveCount
|
||
if asteriskCount > 0 {
|
||
sb.WriteString(strings.Repeat(string(asteriskChar), asteriskCount))
|
||
}
|
||
// 写入后缀
|
||
if endReserveCount > 0 && length-endReserveCount >= startReserveCount { // 确保后缀不与前缀重叠
|
||
sb.WriteString(string(runes[length-endReserveCount:]))
|
||
}
|
||
|
||
return sb.String()
|
||
}
|