Documentation
¶
Overview ¶
Package pii 提供 PII(个人身份信息)检测与脱敏功能
本包实现 PII 检测与处理:
- 检测:识别文本中的 PII
- 脱敏:对 PII 进行遮蔽或替换
- 多语言:支持中文和英文
- 可扩展:支持自定义 PII 类型
设计借鉴:
- Microsoft Presidio: PII 检测
- AWS Comprehend: PII 识别
- Google DLP: 数据脱敏
Index ¶
- Variables
- func AnonymizeText(ctx context.Context, text string) (string, error)
- type AnonymizeStrategy
- type Anonymizer
- type Detector
- type HashStrategy
- type MaskStrategy
- type PIIEntity
- type PIIType
- type Pattern
- type ProcessResult
- type Processor
- func (p *Processor) Anonymize(text string, entities []*PIIEntity) string
- func (p *Processor) Detect(ctx context.Context, text string) ([]*PIIEntity, error)
- func (p *Processor) Process(ctx context.Context, text string) (*ProcessResult, error)
- func (p *Processor) SetAnonymizer(anonymizer *Anonymizer)
- func (p *Processor) SetDetector(detector Detector)
- type ProcessorConfig
- type RegexDetector
- type ReplaceStrategy
Constants ¶
This section is empty.
Variables ¶
View Source
var DefaultProcessorConfig = ProcessorConfig{ MinScore: 0.8, AutoAnonymize: true, }
DefaultProcessorConfig 默认配置
Functions ¶
Types ¶
type AnonymizeStrategy ¶
AnonymizeStrategy 脱敏策略
type Anonymizer ¶
type Anonymizer struct {
// contains filtered or unexported fields
}
Anonymizer 脱敏器
func (*Anonymizer) Anonymize ¶
func (a *Anonymizer) Anonymize(text string, entities []*PIIEntity) string
Anonymize 执行脱敏
区间在替换前会被规整为互不重叠:先按起点升序合并任意重叠区间(避免部分重叠的检测 结果在替换时相互错位、把原始片段重新拼回输出),再从后往前替换。每个区间的 Value 按其字节范围从原文重新截取,使脱敏掩码长度与被替换区间一致,杜绝原始 PII 片段残留。
func (*Anonymizer) SetStrategy ¶
func (a *Anonymizer) SetStrategy(piiType PIIType, strategy AnonymizeStrategy)
SetStrategy 设置策略
type Detector ¶
type Detector interface {
// Detect 检测 PII
Detect(ctx context.Context, text string) ([]*PIIEntity, error)
// SupportedTypes 支持的 PII 类型
SupportedTypes() []PIIType
}
Detector PII 检测器接口
type HashStrategy ¶
type HashStrategy struct {
// Prefix 前缀
Prefix string
// Length 长度(用于截取原始值的前 N 个字符进行哈希)
// 如果 Length <= 0 或大于原始值长度,将使用整个值
Length int
}
HashStrategy 哈希策略
func (*HashStrategy) Anonymize ¶
func (s *HashStrategy) Anonymize(entity *PIIEntity) string
Anonymize 哈希脱敏
type MaskStrategy ¶
type MaskStrategy struct {
// MaskChar 遮蔽字符
MaskChar rune
// PreserveFirst 保留前几位
PreserveFirst int
// PreserveLast 保留后几位
PreserveLast int
}
MaskStrategy 遮蔽策略
func (*MaskStrategy) Anonymize ¶
func (s *MaskStrategy) Anonymize(entity *PIIEntity) string
Anonymize 遮蔽脱敏
type PIIEntity ¶
type PIIEntity struct {
// Type PII 类型
Type PIIType `json:"type"`
// Value 原始值
Value string `json:"value"`
// Start 起始位置
Start int `json:"start"`
// End 结束位置
End int `json:"end"`
// Score 置信度(0-1)
Score float64 `json:"score"`
// Metadata 元数据
Metadata map[string]any `json:"metadata,omitempty"`
}
PIIEntity 检测到的 PII 实体
type PIIType ¶
type PIIType string
PIIType PII 类型
const ( // PIITypePhone 电话号码 PIITypePhone PIIType = "phone" // PIITypeEmail 邮箱地址 PIITypeEmail PIIType = "email" // PIITypeIDCard 身份证号 PIITypeIDCard PIIType = "id_card" // PIITypeCreditCard 信用卡号 PIITypeCreditCard PIIType = "credit_card" // PIITypeBankAccount 银行账号 PIITypeBankAccount PIIType = "bank_account" // PIITypeName 姓名 PIITypeName PIIType = "name" // PIITypeAddress 地址 PIITypeAddress PIIType = "address" // PIITypePassport 护照号 PIITypePassport PIIType = "passport" // PIITypeSSN 社会安全号(美国) PIITypeSSN PIIType = "ssn" // PIITypeLicense 驾照号 PIITypeLicense PIIType = "license" // PIITypeIP IP 地址 PIITypeIP PIIType = "ip" // PIITypeMac MAC 地址 PIITypeMac PIIType = "mac" // PIITypeURL URL PIITypeURL PIIType = "url" // PIITypeDate 日期 PIITypeDate PIIType = "date" // PIITypeCustom 自定义类型 PIITypeCustom PIIType = "custom" )
type Pattern ¶
type Pattern struct {
// Name 模式名称
Name string
// Regex 正则表达式
Regex *regexp.Regexp
// Score 默认置信度
Score float64
// Validator 验证函数(可选)
Validator func(match string) bool
}
Pattern 正则模式
type ProcessResult ¶
type ProcessResult struct {
// OriginalText 原始文本
OriginalText string `json:"original_text"`
// AnonymizedText 脱敏后文本
AnonymizedText string `json:"anonymized_text,omitempty"`
// Entities 检测到的实体
Entities []*PIIEntity `json:"entities"`
// HasPII 是否包含 PII
HasPII bool `json:"has_pii"`
}
ProcessResult 处理结果
type Processor ¶
type Processor struct {
// contains filtered or unexported fields
}
Processor PII 处理器
func (*Processor) SetAnonymizer ¶
func (p *Processor) SetAnonymizer(anonymizer *Anonymizer)
SetAnonymizer 设置脱敏器
func (*Processor) SetDetector ¶
SetDetector 设置检测器
type ProcessorConfig ¶
type ProcessorConfig struct {
// EnabledTypes 启用的 PII 类型(空表示全部)
EnabledTypes []PIIType
// MinScore 最小置信度
MinScore float64
// AutoAnonymize 自动脱敏
AutoAnonymize bool
}
ProcessorConfig 处理器配置
type RegexDetector ¶
type RegexDetector struct {
// contains filtered or unexported fields
}
RegexDetector 基于正则表达式的检测器
func (*RegexDetector) RegisterPattern ¶
func (d *RegexDetector) RegisterPattern(piiType PIIType, pattern *Pattern)
RegisterPattern 注册模式
func (*RegexDetector) SupportedTypes ¶
func (d *RegexDetector) SupportedTypes() []PIIType
SupportedTypes 支持的类型
type ReplaceStrategy ¶
type ReplaceStrategy struct {
// Replacement 替换文本
Replacement string
}
ReplaceStrategy 替换策略
func (*ReplaceStrategy) Anonymize ¶
func (s *ReplaceStrategy) Anonymize(entity *PIIEntity) string
Anonymize 替换脱敏
Click to show internal directories.
Click to hide internal directories.