-
Notifications
You must be signed in to change notification settings - Fork 0
/
masker.go
232 lines (192 loc) · 5.28 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
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
package masker
import (
"strings"
)
// Masker структура для маскировки строк
type Masker struct{}
// mask заменяет символы в строке str на mask с позиции start до позиции end
func (m *Masker) mask(str string, overlay string, start int, end int) string {
r := []rune(str)
l := len(r)
if l == 0 {
return ""
}
if start < 0 {
start = 0
}
if start > l {
start = l
}
if end < 0 {
end = 0
}
if end > l {
end = l
}
if start > end {
start, end = end, start
}
return string(r[:start]) + overlay + string(r[end:])
}
// maskExceptFirstLast оставляет только первый и последний символ
// использовать только со строками из ASCII символов (https://ru.wikipedia.org/wiki/ASCII)
func (m *Masker) maskExceptFirstLast(str string, overlay string, needLast bool) string {
bytes := make([]byte, len(str))
for i, k := range []byte(str) {
if i == 0 || (i == len(str)-1 && needLast) {
bytes[i] = k
continue
}
bytes[i] = []byte(overlay)[0]
}
return string(bytes)
}
// maskExceptLastDigits оставляет count последних символов
// использовать только со строками из ASCII символов (https://ru.wikipedia.org/wiki/ASCII)
func (m *Masker) maskExceptLastDigits(str string, overlay string, count int) string {
bytes := make([]byte, len(str))
for i, k := range []byte(str) {
if i >= len(str)-count {
bytes[i] = k
continue
}
bytes[i] = []byte(overlay)[0]
}
return string(bytes)
}
// Name маскирует второй и третий символ в строке. Может работать со строками из нескольких слов
func (m *Masker) Name(i string) string {
l := len([]rune(i))
if l == 0 {
return ""
}
// если есть пробел
i = strings.Trim(i, " ")
if chunks := strings.Split(i, " "); len(chunks) > 1 {
tmp := make([]string, 0, l)
for _, v := range chunks {
if v != "" {
tmp = append(tmp, m.Name(v))
}
}
return strings.Join(tmp, " ")
}
if l == 2 || l == 3 {
return m.mask(i, "*", 1, 2)
}
if l > 3 {
return m.mask(i, "**", 1, 3)
}
return "*"
}
// CreditCard маскирует 6 символов номера кредитной карты начиная с 7
func (m *Masker) CreditCard(i string) string {
if len(i) == 0 {
return ""
}
return m.mask(i, "******", 6, 12)
}
// Email маскирует логин в email, оставляя домен
func (m *Masker) Email(i string) string {
if len(i) == 0 {
return ""
}
chunks := strings.Split(i, "@")
return m.mask(chunks[0], "****", 3, 7) + "@" + chunks[1]
}
// Mobile маскирует 11 значный номер телефона с ведущей цифрой 7
// оставляет первые 4 и последние 2 цифры
func (m *Masker) Mobile(i string) string {
if len(i) == 0 {
return ""
}
return m.mask(i, "*****", 4, 9)
}
// Password маскирует пароль заданной маской
func (m *Masker) Password(i string) string {
l := len([]rune(i))
if l == 0 {
return ""
}
return "************"
}
// PassportSeries маскирует серию паспорта, оставляя первую и последнюю цифры
func (m *Masker) PassportSeries(i string) string {
if len(i) == 0 {
return ""
}
if len(i) < 4 {
return "****"
}
return m.maskExceptFirstLast(i, "*", true)
}
// PassportNumber маскирует номер паспорта, оставляя первую и последнюю цифру
func (m *Masker) PassportNumber(i string) string {
if len(i) == 0 {
return ""
}
if len(i) < 6 {
return "******"
}
return m.maskExceptFirstLast(i, "*", true)
}
// Code маскирует код из цифр
// для кодов состоящих из меньше чем 4 символов, оставляет первый символ
// для кодов большей длины оставляет первый и последний символ
func (m *Masker) Code(i string) string {
if len(i) == 0 {
return ""
}
if len(i) == 1 {
return "*"
}
if len(i) < 4 {
return m.maskExceptFirstLast(i, "*", false)
}
return m.maskExceptFirstLast(i, "*", true)
}
//LastFourDigits маскирует любую последовательность больше 5 символов, оставляя 4 последних символа
func (m *Masker) LastFourDigits(i string) string {
if len(i) == 0 {
return ""
}
if len(i) < 5 {
return "****"
}
return m.maskExceptLastDigits(i, "*", 4)
}
// New создает Masker
func New() *Masker {
return &Masker{}
}
var instance *Masker
func init() {
instance = New()
}
func Name(i string) string {
return instance.Name(i)
}
func CreditCard(i string) string {
return instance.CreditCard(i)
}
func Email(i string) string {
return instance.Email(i)
}
func Mobile(i string) string {
return instance.Mobile(i)
}
func Password(i string) string {
return instance.Password(i)
}
func PassportSeries(i string) string {
return instance.PassportSeries(i)
}
func PassportNumber(i string) string {
return instance.PassportNumber(i)
}
func Code(i string) string {
return instance.Code(i)
}
func LastFourDigits(i string) string {
return instance.LastFourDigits(i)
}