-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmasker.go
35 lines (30 loc) · 1.02 KB
/
masker.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
package minimizers
import (
"github.com/adaptant-labs/go-minimizer/datagen"
"unicode"
)
// Mask randomly swaps out alphanumeric values within a string with type-matching random values. The format (and case)
// of the string is preserved, while special characters are left in-place.
// e.g. Mask("123-456-7890") => "155-503-1463"
func Mask(input string) (string, error) {
output := []rune(input)
for i, c := range input {
if unicode.IsLetter(c) {
output[i] = datagen.GenerateRandomLetter(unicode.IsUpper(c))
} else if unicode.IsNumber(c) {
output[i] = datagen.GenerateRandomDigit()
}
}
return string(output), nil
}
// MaskWithPattern provides a masking variant in which all alphanumeric characters are replaced with a given pattern.
// e.g. MaskWithPattern("123-456-7890", 'X') => "XXX-XXX-XXXX".
func MaskWithPattern(input string, pattern rune) (string, error) {
output := []rune(input)
for i, c := range input {
if unicode.IsLetter(c) || unicode.IsNumber(c) {
output[i] = pattern
}
}
return string(output), nil
}