-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredactor.go
57 lines (50 loc) · 1.17 KB
/
redactor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package redact
func (this *Redactor) RedactAll(input []byte) []byte {
this.clear(this.phone, this.email, this.dob, this.credit, this.ssn)
length := len(input)
if length <= 0 {
return input
}
if length > this.maxLength {
input = input[0:this.maxLength]
}
this.match(input, this.phone, this.email, this.ssn, this.dob, this.credit)
result := this.redactMatches(input)
return result
}
func (this *Redactor) redactMatches(input []byte) []byte {
count := len(this.matches)
if count == 0 {
return input
}
this.monitor.Redacted(count)
buffer := input
bufferLength := len(buffer)
var lowIndex, highIndex int
for _, match := range this.matches {
lowIndex = match.InputIndex
highIndex = lowIndex + match.Length
if lowIndex < 0 {
continue
}
if highIndex > bufferLength {
continue
}
for ; lowIndex < highIndex; lowIndex++ {
buffer[lowIndex] = '*'
}
}
output := buffer
return output
}
func (this *Redactor) match(input []byte, matchMethod ...Redaction) {
for _, method := range matchMethod {
method.match(input)
}
}
func (this *Redactor) clear(matchMethod ...Redaction) {
for _, method := range matchMethod {
method.clear()
}
this.matched.clear()
}