-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunegen.go
285 lines (246 loc) · 7.62 KB
/
runegen.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
/*
Copyright © 2022 Sallehuddin Abdul Latif sallehuddin@berrypay.com
*/
// Package runegen provides libraries for generating random string from specified rune/runes.
package runegen
import (
crand "crypto/rand"
"math/big"
"math/rand"
"strings"
"time"
)
var SmallCaps = "abcdefghijklmnopqrstuvwxyz"
var BigCaps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var Numerics = "0123456789"
var Symbols = "._#@%&!~|$^*="
// GetRandom returns a random string consists of runes from predefined character set/sets.
// Rune set to use are represented by binary representation of the set [bbbbb] where:
//
// 0b00001 => numbers
// 0b00010 => small letters
// 0b00100 => capital letters
// 0b01000 => symbols ._#@%&!~|$^*=
//
// It will use numbers, small letters and capital letters if the rune set specified not matched.
func GetRandom(charSet uint8, n uint) string {
switch charSet {
case 1:
return generateRandom([]rune(Numerics), n)
case 2:
return generateRandom([]rune(SmallCaps), n)
case 3:
return generateRandom([]rune(SmallCaps+Numerics), n)
case 4:
return generateRandom([]rune(BigCaps), n)
case 5:
return generateRandom([]rune(BigCaps+Numerics), n)
case 6:
return generateRandom([]rune(BigCaps+SmallCaps), n)
case 7:
return generateRandom([]rune(BigCaps+SmallCaps+Numerics), n)
case 8:
return generateRandom([]rune(Symbols), n)
case 10:
return generateRandom([]rune(SmallCaps+Symbols), n)
case 11:
return generateRandom([]rune(BigCaps+SmallCaps+Symbols), n)
case 12:
return generateRandom([]rune(BigCaps+Symbols), n)
case 13:
return generateRandom([]rune(BigCaps+Numerics+Symbols), n)
case 14:
return generateRandom([]rune(BigCaps+SmallCaps+Symbols), n)
case 15:
return generateRandom([]rune(BigCaps+SmallCaps+Numerics+Symbols), n)
default:
return generateRandom([]rune(BigCaps+SmallCaps+Numerics), n)
}
}
// GetPolicyRandom returns a random string of length n consists of runes from predefined or custom character set/sets
// complying with the complexity requirements.
//
// num = true => use numeric set
// sl = true => use small letters set
// bl = true => use capital letters set
// sym = true => use symbols set
// strict = true => make sure all rune set selected exist in the resulting string
// startAlpha = true => make sure the resulting string start with an alphabet character
func GetPolicyRandom(num bool, sl bool, bl bool, sym bool, strict bool, startAlpha bool, n uint) string {
policyAlpha := startAlpha
policyStrict := strict
var curRune []rune
if num {
curRune = []rune(string(curRune) + Numerics)
}
if sl {
curRune = []rune(string(curRune) + SmallCaps)
}
if bl {
curRune = []rune(string(curRune) + BigCaps)
}
if sym {
curRune = []rune(string(curRune) + Symbols)
}
if !num && !sl && !bl && !sym {
// we won't allow empty runes... if all not set, then use all runes
curRune = []rune(Numerics + SmallCaps + BigCaps + Symbols)
}
var alphaFirst bool
if !sl && !bl {
policyAlpha = false
}
var generated string
// No strict checking requested
if !policyStrict {
if policyAlpha {
for !alphaFirst {
generated = generateRandom(curRune, n)
alphaFirst = startedWithAlpha(generated)
//fmt.Printf("Generated: %v and start with alpha: %v\n", generated, alphaFirst)
}
return generated
}
return generateRandom(curRune, n)
}
// We will reach this part when strict checking was requested
hasNum, hasSL, hasBL, hasSym := false, false, false, false
if policyAlpha {
for !hasNum || !hasSL || !hasBL || !hasSym || !alphaFirst {
generated = generateRandom(curRune, n)
hasNum = numericExist(generated)
//overwrite if numeric is not in the runes selection
if !num {
hasNum = true
}
hasSL = smallLetterExist(generated)
//overwrite if small letters is not in the runes selection
if !sl {
hasSL = true
}
hasBL = capitalLetterExist(generated)
//overwrite if small letters is not in the runes selection
if !bl {
hasBL = true
}
hasSym = symbolExist(generated)
//overwrite if small letters is not in the runes selection
if !sym {
hasSym = true
}
alphaFirst = startedWithAlpha(generated)
//overwrite if small letters or capital letters are not in the runes selection
if !sl && !bl {
alphaFirst = true
}
//fmt.Printf("Generated: %v and policy result: %v|%v|%v|%v|%v\n", generated, hasNum, hasSL, hasBL, hasSym, alphaFirst)
}
} else {
for !hasNum || !hasSL || !hasBL || !hasSym {
generated = generateRandom(curRune, n)
hasNum = numericExist(generated)
//overwrite if numeric is not in the runes selection
if !num {
hasNum = true
}
hasSL = smallLetterExist(generated)
//overwrite if small letters is not in the runes selection
if !sl {
hasSL = true
}
hasBL = capitalLetterExist(generated)
//overwrite if small letters is not in the runes selection
if !bl {
hasBL = true
}
hasSym = symbolExist(generated)
//overwrite if small letters is not in the runes selection
if !sym {
hasSym = true
}
//fmt.Printf("Generated: %v and policy result: %v|%v|%v|%v\n", generated, hasNum, hasSL, hasBL, hasSym)
}
}
return generated
}
// GetCustomRandom returns a random string of length n consists of runes from the specified string of characters s.
func GetCustomRandom(s string, n uint) string {
return generateRandom([]rune(s), n)
}
// generateRandom returns a random string of length n consist of runes from the given set.
func generateRandom(r []rune, n uint) string {
randomizer := rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]rune, n)
for i := range b {
b[i] = r[randomizer.Intn(len(r))]
}
return string(b)
}
// numericExist returns a boolean indicating whether at least single numeric character exist in the string s
func numericExist(s string) bool {
isExist := false
testRunes := []rune(Numerics)
for i := 0; i < len(testRunes); i++ {
char := string(testRunes[i])
if strings.Contains(s, char) {
isExist = true
}
}
return isExist
}
// smallLetterExist returns a boolean indicating whether at least single small letter character exist in the string s
func smallLetterExist(s string) bool {
isExist := false
testRunes := []rune(SmallCaps)
for i := 0; i < len(testRunes); i++ {
char := string(testRunes[i])
if strings.Contains(s, char) {
isExist = true
}
}
return isExist
}
// capitalLetterExist returns a boolean indicating whether at least single capital letter character exist in the string s
func capitalLetterExist(s string) bool {
isExist := false
testRunes := []rune(BigCaps)
for i := 0; i < len(testRunes); i++ {
char := string(testRunes[i])
if strings.Contains(s, char) {
isExist = true
}
}
return isExist
}
// symbolExist returns a boolean indicating whether at least single symbol character exist in the string s
func symbolExist(s string) bool {
isExist := false
testRunes := []rune(Symbols)
for i := 0; i < len(testRunes); i++ {
char := string(testRunes[i])
if strings.Contains(s, char) {
isExist = true
}
}
return isExist
}
// startedWithAlpha returns a boolean indicating whether the string s starts with an alphabet character
func startedWithAlpha(s string) bool {
return strings.Contains(SmallCaps+BigCaps, s[0:1])
}
// Nonce Generator
// Generate random string Nonce of length n using crypto/rand
func GetNonceStr(n uint) string {
runes := []rune(SmallCaps + BigCaps + Numerics)
b := make([]rune, n)
for i := range b {
random, err := crand.Int(crand.Reader, big.NewInt(int64(len(runes))))
if err != nil {
//fallback to use math/rand
randomizer := rand.New(rand.NewSource(time.Now().UnixNano()))
random = big.NewInt(int64(randomizer.Intn(len(runes))))
}
b[i] = runes[random.Int64()]
}
return string(b)
}