RedactWithMasking 使用掩码脱敏
(text string, maskLength int)
| 78 | |
| 79 | // RedactWithMasking 使用掩码脱敏 |
| 80 | func (r *PIIRedactor) RedactWithMasking(text string, maskLength int) string { |
| 81 | if r.detector == nil || text == "" { |
| 82 | return text |
| 83 | } |
| 84 | |
| 85 | ctx := context.Background() |
| 86 | matches, err := r.detector.Detect(ctx, text) |
| 87 | if err != nil || len(matches) == 0 { |
| 88 | return text |
| 89 | } |
| 90 | |
| 91 | // 按位置倒序排列 |
| 92 | for i := len(matches) - 1; i >= 0; i-- { |
| 93 | match := matches[i] |
| 94 | maskedValue := r.maskValue(match.Value, maskLength) |
| 95 | text = text[:match.Start] + maskedValue + text[match.End:] |
| 96 | } |
| 97 | |
| 98 | return text |
| 99 | } |
| 100 | |
| 101 | // maskValue 创建掩码值 |
| 102 | func (r *PIIRedactor) maskValue(value string, maskLength int) string { |