-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandstring.go
More file actions
229 lines (187 loc) · 6.46 KB
/
randstring.go
File metadata and controls
229 lines (187 loc) · 6.46 KB
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
package randstring
import (
"errors"
"math/rand"
"strings"
"time"
)
// RandomStr is a structure providing the criteria when creating the random string
type RandomStr struct {
// Length is the number of characters of the random string.
// This excludes any prefix or posfix string length.
Length int
// CapitalsFlag determines if there should be at least one capital letter character in the
// random string generated.
CapitalsFlag bool
// CapitalChars provides the list of capital characters to choose from when generating
// the random string.
CapitalChars []rune
// LowercaseFlag determines if there should be at least one lowercase character in the
// random string generated.
LowercaseFlag bool
// LowercaseChars provides the list of lowercase characters to choose from when generating
// the random string.
LowercaseChars []rune
// DigitsFlag determines if there should be at least one numeral character in the
// random string generated.
DigitsFlag bool
// DigitChars provides the list of numeral characters to choose from when generating
// the random string.
DigitChars string
// SpecialsFlag determines if there should be at least one special character in the
// random string generated.
SpecialsFlag bool
// SpecialChars provides the list of special characters to choose from when generating
// the random string.
SpecialChars string
// Prefix provides a list of characters that should be added to the beginning of the random
// string.
Prefix []rune
// Posfix provides a list of characters that should be added to the end of the random
// string.
Posfix []rune
}
// New creates a new RandomStr structure with default values.
func New(length int) RandomStr {
return RandomStr{
Length: length,
Prefix: []rune(""),
Posfix: []rune(""),
LowercaseFlag: true,
CapitalsFlag: true,
DigitsFlag: true,
SpecialsFlag: true,
CapitalChars: []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
LowercaseChars: []rune("abcdefghijklmnopqrstuvwxyz"),
DigitChars: "0123456789",
SpecialChars: "^$*.[]{}()?-!@#%&/,><:;|_~=+",
}
}
// SetPrefix sets a prefix value to be used as part of the string creation: [prefix][random string].
func (r *RandomStr) SetPrefix(prefix []rune) {
r.Prefix = prefix
}
// SetPosfix sets the posfix value to be used as part of the string creation: [random string][posfix].
func (r *RandomStr) SetPosfix(posfix []rune) {
r.Posfix = posfix
}
// Capitals determines whether there should be at least one capital letter character in the random string.
func (r *RandomStr) Capitals(flag bool) {
r.CapitalsFlag = flag
}
// Lowercase determines whether there should be at least one lowercase character in the random string.
func (r *RandomStr) Lowercase(flag bool) {
r.LowercaseFlag = flag
}
// Digits determines whether there should be at least one numeral character in the random string.
func (r *RandomStr) Digits(flag bool) {
r.DigitsFlag = flag
}
// Specials determines whether there should be at least one special character in the random string.
func (r *RandomStr) Specials(flag bool) {
r.SpecialsFlag = flag
}
// SetCapitalChars sets the list of capital characters to be used as part of the random string generation.
func (r *RandomStr) SetCapitalChars(chars []rune) {
r.CapitalChars = chars
}
// SetLowercaseChars sets the list of lowercase characters to be used as part of the random string generation.
func (r *RandomStr) SetLowercaseChars(chars []rune) {
r.LowercaseChars = chars
}
// SetDigitChars sets the list of number characters to be used as part of the random string generation
func (r *RandomStr) SetDigitChars(chars string) {
r.DigitChars = chars
}
// SetSpecialChars sets the list of special characters to be used as part of the random string generation
func (r *RandomStr) SetSpecialChars(chars string) {
r.SpecialChars = chars
}
func (r *RandomStr) requireCapitals() bool {
return r.CapitalsFlag && len(r.CapitalChars) > 0
}
func (r *RandomStr) requireLowercase() bool {
return r.LowercaseFlag && len(r.LowercaseChars) > 0
}
func (r *RandomStr) requireDigits() bool {
return r.DigitsFlag && len(r.DigitChars) > 0
}
func (r *RandomStr) requireSpecialChars() bool {
return r.SpecialsFlag && len(r.SpecialChars) > 0
}
func (r *RandomStr) buildAvailable() ([]rune, int) {
var all []rune
i := 0
if r.requireCapitals() {
all = append(all, []rune(r.CapitalChars)...)
i++
}
if r.requireLowercase() {
all = append(all, []rune(r.LowercaseChars)...)
i++
}
if r.requireDigits() {
all = append(all, []rune(r.DigitChars)...)
i++
}
if r.requireSpecialChars() {
all = append(all, []rune(r.SpecialChars)...)
i++
}
return all, i
}
func (r *RandomStr) check() error {
if r.CapitalsFlag && len(r.CapitalChars) == 0 {
return errors.New("Restriction requires to have capitals, however no capital char list has been set")
}
if r.LowercaseFlag && len(r.LowercaseChars) == 0 {
return errors.New("Restriction requires to have lowercase letters, however no lowercase char list has been set")
}
if r.DigitsFlag && len(r.DigitChars) == 0 {
return errors.New("Restriction requires to have digits, however no digit char list has been set")
}
if r.SpecialsFlag && len(r.SpecialChars) == 0 {
return errors.New("Restriction requires to have special characters, however no special char list has been set")
}
_, minLength := r.buildAvailable()
if r.Length < minLength {
return errors.New("The minimum length of the random string requires to be [" + string(minLength) + "] based on the restrictions selected")
}
return nil
}
// Build generates a random string based on the set of criteria.
func (r *RandomStr) Build() (string, error) {
err := r.check()
if err != nil {
return "", err
}
rand.Seed(time.Now().UnixNano())
all, _ := r.buildAvailable()
index := 0
var b strings.Builder
if r.requireCapitals() {
b.WriteRune(r.CapitalChars[rand.Intn(len(r.CapitalChars))])
index++
}
if r.requireLowercase() {
b.WriteRune(r.LowercaseChars[rand.Intn(len(r.LowercaseChars))])
index++
}
if r.requireDigits() {
b.WriteByte(r.DigitChars[rand.Intn(len(r.DigitChars))])
index++
}
if r.requireSpecialChars() {
b.WriteByte(r.SpecialChars[rand.Intn(len(r.SpecialChars))])
index++
}
// fill the rest of the restricted length from any of the available charaters
for i := index; i < r.Length; i++ {
b.WriteRune(all[rand.Intn(len(all))])
}
buf := []rune(b.String())
rand.Shuffle(len(buf), func(i, j int) {
buf[i], buf[j] = buf[j], buf[i]
})
return string(r.Prefix) + string(buf) + string(r.Posfix), nil
}