forked from logdan/big-mask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
masking.go
439 lines (387 loc) · 12.3 KB
/
masking.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package main
// fast
//
//
//
// Inputs: [1] XML file, dataset to mask
// [2] XLS file, all values to be replaced WITHOUT HEADERS
// [3] XLS file, seed values (first character of input -:> output)
// [4] string, filename of the output xml
import (
"bufio"
"bytes"
"fmt"
"io"
"math/rand"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/tealeg/xlsx"
"github.com/vbauerster/mpb"
"github.com/vbauerster/mpb/decor"
)
const (
A9 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // String of possible chars for random string generator
letterIdxBits = 6 // 6 bits to represent a letter index
specialCharactersRegex = "[<>&'\"]" // These characters get converted in XML, so we need to split around them
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
var src = rand.NewSource(time.Now().UnixNano())
var maskIntDelimiter = 77 // Surround masked ints with 77 on either side
var smallestMaskingAmount = 2 // Don't mask chunks smaller than 2 characters
type maskstring struct {
value string
masked bool
}
func initCmdLineArgs() (string, string, string, string) {
if len(os.Args) != 5 {
_, _ = fmt.Printf("Invalid number of args. Need 4, got %d\n", len(os.Args)-1)
_, _ = fmt.Println("example usage: > masking dataset.xml datatomask.xlsx seeds.xlsx outputname.xml ")
os.Exit(3)
}
datasetFilename := os.Args[1]
valuesToReplaceFilename := os.Args[2]
seedFilename := os.Args[3]
outputFilename := os.Args[4]
return datasetFilename, valuesToReplaceFilename, seedFilename, outputFilename
}
func initSeeds(seedFilepath string) map[string]string {
seedXls, err := xlsx.OpenFile(seedFilepath)
if err != nil {
print(err.Error())
os.Exit(45)
}
seedSheet := seedXls.Sheets[0]
seeds := make(map[string]string)
seedRows := seedSheet.Rows
for i := 0; i < len(seedSheet.Rows); i++ {
key := seedRows[i].Cells[0].Value
value := seedRows[i].Cells[1].Value
seeds[key] = value
}
return seeds
}
// Generate a new string based on seed and counter. Allows for some control (so that not every ID is just completely random)
func generateNewValue(seed string, counter int, desiredLength int) string {
genString := ""
if desiredLength > smallestMaskingAmount {
genString = strings.Join([]string{seed, strconv.Itoa(counter), "M"}, "")
} else { // still unique, just shorter
genString = generateUnique4CharString(counter)
}
return genString
}
func importMappedValuesFromFile(excelFilepath string) (map[string][]maskstring, error) {
valuesXls, err := xlsx.OpenFile(excelFilepath)
if err != nil {
return nil, err
}
valuesSheet := valuesXls.Sheets[0]
maskValuesInterim := make(map[string][]maskstring)
valuesRows := valuesSheet.Rows
descriptionRegex := regexp.MustCompile(specialCharactersRegex)
// Initialize by setting keys and values equal so we can mask strings piece by piece
for _, row := range valuesRows {
for _, cell := range row.Cells {
old := cell.Value
if len(old) >= smallestMaskingAmount || !(len(maskValuesInterim[old]) > 0) {
splitold := descriptionRegex.Split(old, -1)
if len(splitold) == 1 {
maskValuesInterim[old] = []maskstring{{old, false}}
} else {
for _, val := range splitold {
if len(val) > smallestMaskingAmount {
maskValuesInterim[val] = []maskstring{{val, false}}
}
}
}
}
}
}
return maskValuesInterim, nil
}
func initMaskValues(maskValuesInterim map[string][]maskstring, seedmap map[string]string) map[string]string {
maskValuesResolved := make(map[string]string)
//Iterate from smallest to largest
var sortedKeys []string
for k := range maskValuesInterim {
sortedKeys = append(sortedKeys, k)
}
sort.SliceStable(sortedKeys, func(i, j int) bool {
return len(sortedKeys[i]) < len(sortedKeys[j])
})
counter := 1000
for i, keyVal := range sortedKeys {
// As we mask strings, this holds the strings which the keyValue will be mapped to in the end
var stringsToJoin []string
// If it's an int, mask it as one piece and pass
if isInt(keyVal) {
newString := strconv.Itoa(maskIntDelimiter) + strconv.Itoa(counter) + strconv.Itoa(maskIntDelimiter)
maskValuesInterim[keyVal] = []maskstring{{newString, true}}
stringsToJoin = []string{newString}
counter++
} else {
//Mask the values of the current keyval
for z, mstring := range maskValuesInterim[keyVal] {
if !mstring.masked && len(mstring.value) > 0 {
//check that string isn't some character we don't want to mask, or too short
seedchar := string(mstring.value[0])
seed := seedmap[seedchar]
newValue := generateNewValue(seed, counter, len(mstring.value))
maskValuesInterim[keyVal][z].value = newValue
maskValuesInterim[keyVal][z].masked = true
counter++
}
stringsToJoin = append(stringsToJoin, maskValuesInterim[keyVal][z].value)
}
}
maskValuesResolved[keyVal] = strings.Join(stringsToJoin, "")
// loop through all future keyvals and change the vals to what we want
for _, afterKeyVal := range sortedKeys[i+1:] {
var newArray []maskstring
for z, mstring := range maskValuesInterim[afterKeyVal] {
if strings.Contains(mstring.value, keyVal) {
//Split into 3 slices, middle part contains value that we found
valueToSplit := maskValuesInterim[afterKeyVal][z].value
start := strings.Index(valueToSplit, keyVal)
end := start + len(keyVal)
firstPart := maskstring{valueToSplit[:start], false} // what came before
middlePart := maskstring{valueToSplit[start:end], false} // string to replace
endPart := maskstring{valueToSplit[end:], false} // what comes after
// now change middlepart's value to the masked value of what we found
middlePart.value = maskValuesResolved[keyVal]
middlePart.masked = true
//append to newArray
newArray = append(newArray, firstPart, middlePart, endPart)
} else {
newArray = append(newArray, maskValuesInterim[afterKeyVal][z])
}
}
maskValuesInterim[afterKeyVal] = newArray
}
counter++
}
return maskValuesResolved
}
// Iterates through dataset xml, writes each edited line to a new file
func maskDataset(datasetFilepath string, maskedValues map[string]string, outputFilename string) int {
maskingStart := time.Now()
fileLength, _ := getFileLength(datasetFilepath)
_, _ = fmt.Printf("The file is %d lines long\n", fileLength)
bar := initProgressBar(fileLength)
onePercent := fileLength / 100
dataset, _ := os.Open(datasetFilepath)
reader := bufio.NewReader(dataset)
output, _ := os.Create(outputFilename)
writer := bufio.NewWriter(output)
counter := 0
var fromTokens = make([]string, len(maskedValues))
var toTokens = make([]string, len(maskedValues))
var i = 0
for k := range maskedValues {
fromTokens[i] = k
i++
}
//sort from tokens longest to shortest, then populate toTokens with values from those keys
sort.SliceStable(fromTokens, func(i, j int) bool {
return len(fromTokens[i]) > len(fromTokens[j])
})
for i, key := range fromTokens {
toTokens[i] = maskedValues[key]
i++
}
keyValPairs := zip(fromTokens, toTokens)
replacer := strings.NewReplacer(keyValPairs...)
// mask any xml line with Description specifically
descriptionRegex, _ := regexp.Compile("<Description>(.*)</Description>")
for {
line, _, err := reader.ReadLine()
linetext := string(line)
newline := descriptionRegex.ReplaceAllString(linetext, "<Description>MASKED_DESCRIPTION</Description>")
newline = replacer.Replace(newline)
_, _ = writer.WriteString(newline + "\n")
if err == io.EOF {
break
}
counter++
// Every one percent, update the progress bar
if counter%onePercent == 0 {
bar.IncrBy(onePercent, time.Since(maskingStart))
}
if fileLength-counter == 3 {
bar.Completed()
}
// Print out the last two lines
if fileLength-counter < 2 {
fmt.Println(newline)
}
}
_ = writer.Flush()
return counter
}
// Quick count of # of lines in a file [imported]
func getFileLength(filepath string) (int, error) {
r, _ := os.Open(filepath)
buf := make([]byte, 32*1024)
count := 0
lineSep := []byte{'\n'}
for {
c, err := r.Read(buf)
count += bytes.Count(buf[:c], lineSep)
switch {
case err == io.EOF:
return count, nil
case err != nil:
return count, err
}
}
}
func main() {
start := time.Now() // For timing
datasetFilepath, valuesFilepath, seedPath, outputFilename := initCmdLineArgs()
println("Command line arguments parsed...")
seeds := initSeeds(seedPath) // Convert seeds into a key-value map of first character to seed value
println("Seeds initialized...")
maskedValuesInit, err := importMappedValuesFromFile(valuesFilepath)
if err != nil {
logrus.Error(err)
}
maskedValues := initMaskValues(maskedValuesInit, seeds) // Generate the masked value for each value needed, in a map
err = printToFileMap("maskedValuesOUT.txt", maskedValues)
if err != nil {
logrus.Error(err)
}
println("Masked values generated...")
count := maskDataset(datasetFilepath, maskedValues, outputFilename) // Iterate through the dataset xml and find/replace each old with each masked
end := time.Since(start)
time.Sleep(time.Second)
println("Masking complete, processed", count, "lines")
println("Output in:", outputFilename)
println("Took", formatDuration(end))
}
// format duration to m:ss.ms
func formatDuration(d time.Duration) string {
d = d.Round(time.Millisecond)
m := d / time.Minute
if m > 0 {
d -= m * time.Minute
}
s := d / time.Second
d -= s * time.Second
ms := d / time.Millisecond
outstring := ""
if m > 0 {
outstring = fmt.Sprintf("%dm%02d.%04ds", m, s, ms)
} else {
outstring = fmt.Sprintf("%d.%04ds", s, ms)
}
return outstring
}
// console progress bar to show percentage complete
func initProgressBar(fileLength int) *mpb.Bar {
p := mpb.New()
total := int64(fileLength)
name := "Masking Progress:"
bar := p.AddBar(total,
mpb.BarStyle("[██░]"),
mpb.PrependDecorators(
// display our name with one space on the right
decor.Name(name, decor.WC{W: len(name) + 1, C: decor.DidentRight}),
// replace ETA decorator with "done" message, OnComplete event
decor.Elapsed(decor.ET_STYLE_GO),
),
mpb.AppendDecorators(decor.Percentage(decor.WCSyncSpace)),
)
return bar
}
// zip two arrays
// zip([a, b, c], [x, y, z]) -> [a, x, b, y, c, z]
func zip(a1, a2 []string) []string {
r := make([]string, 2*len(a1))
for i, e := range a1 {
r[i*2] = e
r[i*2+1] = a2[i]
}
return r
}
func isInt(val string) bool {
_, err := strconv.Atoi(val)
return err == nil
}
func printMapString(maskVals map[string]string) {
for key, val := range maskVals {
println(key, ":", val)
}
}
// print a string array to file line by line
func printToFile(filename string, values []string) error {
output, _ := os.Create(filename)
writer := bufio.NewWriter(output)
for _, val := range values {
_, err := writer.WriteString(val + "\r\n")
if err != nil {
return err
}
}
err := writer.Flush()
return err
}
//print a map string -> string to file line by line like: "key: value"
func printToFileMap(filename string, values map[string]string) error {
output, _ := os.Create(filename)
writer := bufio.NewWriter(output)
var sortedKeys []string
for k := range values {
sortedKeys = append(sortedKeys, k)
}
sort.SliceStable(sortedKeys, func(i, j int) bool {
return len(sortedKeys[i]) < len(sortedKeys[j])
})
for _, key := range sortedKeys {
_, err := writer.WriteString(key + ": " + values[key] + "\r\n")
if err != nil {
return err
}
}
err := writer.Flush()
return err
}
// Generates a unique 4-char string for any value less than 10000. Just a nice to have to not have all mask values be incredibly long
func generateUnique4CharString(seed int) string {
A := int('A')
numCharsInAlphabet := 52
val1 := (seed % numCharsInAlphabet) + A
leftover := seed / numCharsInAlphabet
val2 := (leftover % numCharsInAlphabet) + A
leftover = val2 / numCharsInAlphabet //Maximum of 10000/52/52 = 3.7 = 3
char1 := string(val1)
char2 := string(val2)
char3 := "-"
switch leftover {
case 1:
char3 = "_"
case 2:
char3 = "~"
case 3:
char3 = "-"
case 4:
char3 = "&"
case 5:
char3 = "~"
case 6:
char3 = "]"
case 7:
char3 = "#"
case 8:
char3 = "}"
case 9:
char3 = ","
}
output := char3 + char1 + char2 + char3
return output
}