-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathentity_scrubber.go
267 lines (196 loc) · 6.96 KB
/
entity_scrubber.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package piiscrubber
// EntityScrubber ...
type EntityScrubber interface {
Match(text string) [][]int
Mask(detectedEntity []byte, config *EntityConfig) []byte
}
type mACAddressEntityScrubber struct {
}
func (s *mACAddressEntityScrubber) Match(text string) [][]int {
return MACAddressRegex.FindAllStringIndex(text, -1)
}
func (s *mACAddressEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type mD5HexEntityScrubber struct {
}
func (s *mD5HexEntityScrubber) Match(text string) [][]int {
return MD5HexRegex.FindAllStringIndex(text, -1)
}
func (s *mD5HexEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type iSBNEntityScrubber struct {
}
func (s *iSBNEntityScrubber) Match(text string) [][]int {
indexes := ISBN10Regex.FindAllStringIndex(text, -1)
indexes = append(indexes, ISBN13Regex.FindAllStringIndex(text, -1)...)
return indexes
}
func (s *iSBNEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type iPEntityScrubber struct {
}
func (s *iPEntityScrubber) Match(text string) [][]int {
return IPRegex.FindAllStringIndex(text, -1)
}
func (s *iPEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type iBANEntityScrubber struct {
}
func (s *iBANEntityScrubber) Match(text string) [][]int {
return IBANRegex.FindAllStringIndex(text, -1)
}
func (s *iBANEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type poBoxEntityScrubber struct {
}
func (s *poBoxEntityScrubber) Match(text string) [][]int {
return PoBoxRegex.FindAllStringIndex(text, -1)
}
func (s *poBoxEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type zipCodeEntityScrubber struct {
}
func (s *zipCodeEntityScrubber) Match(text string) [][]int {
return ZipCodeRegex.FindAllStringIndex(text, -1)
}
func (s *zipCodeEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type creditCardEntityScrubber struct {
}
func (s *creditCardEntityScrubber) Match(text string) [][]int {
return CreditCardRegex.FindAllStringIndex(text, -1)
}
func (s *creditCardEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type phoneEntityScrubber struct {
}
func (s *phoneEntityScrubber) Match(text string) [][]int {
indexes := PhonesWithExtsRegex.FindAllStringIndex(text, -1)
indexes = append(indexes, PhoneRegex.FindAllStringIndex(text, -1)...)
return indexes
}
func (s *phoneEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type streetAddressEntityScrubber struct {
}
func (s *streetAddressEntityScrubber) Match(text string) [][]int {
return StreetAddressRegex.FindAllStringIndex(text, -1)
}
func (s *streetAddressEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type sSNEntityScrubber struct {
}
func (s *sSNEntityScrubber) Match(text string) [][]int {
return SSNRegex.FindAllStringIndex(text, -1)
}
func (s *sSNEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type linkEntityScrubber struct {
}
func (s *linkEntityScrubber) Match(text string) [][]int {
return LinkRegex.FindAllStringIndex(text, -1)
}
func (s *linkEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type notKnownPortEntityScrubber struct {
}
func (s *notKnownPortEntityScrubber) Match(text string) [][]int {
return NotKnownPortRegex.FindAllStringIndex(text, -1)
}
func (s *notKnownPortEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type sHA1HexEntityScrubber struct {
}
func (s *sHA1HexEntityScrubber) Match(text string) [][]int {
return SHA1HexRegex.FindAllStringIndex(text, -1)
}
func (s *sHA1HexEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type timeEntityScrubber struct {
}
func (s *timeEntityScrubber) Match(text string) [][]int {
return TimeRegex.FindAllStringIndex(text, -1)
}
func (s *timeEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type dateEntityScrubber struct {
}
func (s *dateEntityScrubber) Match(text string) [][]int {
return DateRegex.FindAllStringIndex(text, -1)
}
func (s *dateEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type sHA256HexEntityScrubber struct {
}
func (s *sHA256HexEntityScrubber) Match(text string) [][]int {
return SHA256HexRegex.FindAllStringIndex(text, -1)
}
func (s *sHA256HexEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type gUIDEntityScrubber struct {
}
func (s *gUIDEntityScrubber) Match(text string) [][]int {
return GUIDRegex.FindAllStringIndex(text, -1)
}
func (s *gUIDEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type emailEntityScrubber struct {
}
func (s *emailEntityScrubber) Match(text string) [][]int {
return EmailRegex.FindAllStringIndex(text, -1)
}
func (s *emailEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type btcAddressEntityScrubber struct {
}
func (s *btcAddressEntityScrubber) Match(text string) [][]int {
return BtcAddressRegex.FindAllStringIndex(text, -1)
}
func (s *btcAddressEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type gitRepoEntityScrubber struct {
}
func (s *gitRepoEntityScrubber) Match(text string) [][]int {
return GitRepoRegex.FindAllStringIndex(text, -1)
}
func (s *gitRepoEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
type strictLinkEntityScrubber struct {
}
func (s *strictLinkEntityScrubber) Match(text string) [][]int {
return StrictLinkRegex.FindAllStringIndex(text, -1)
}
func (s *strictLinkEntityScrubber) Mask(detectedEntity []byte, config *EntityConfig) []byte {
return NativeMasking(detectedEntity, config)
}
// NativeMasking ...
func NativeMasking(detectedEntity []byte, config *EntityConfig) []byte {
if config.ReplaceWith != nil {
return []byte(*config.ReplaceWith)
}
for index := config.UnmaskedPrefixOffset; index < len(detectedEntity)-config.UnmaskedSuffixOffset; index++ {
detectedEntity[index] = byte(*config.MaskWithChar)
}
return detectedEntity
}