-
Notifications
You must be signed in to change notification settings - Fork 0
/
strandom.go
54 lines (44 loc) · 1.06 KB
/
strandom.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
package strandom
import (
"math/rand"
"strings"
"time"
)
var combined []byte
var (
numbers string = "0123456789"
lowerLetters string = "abcdefghijklmnopqrstuvwxyz"
upperLetters string = strings.ToUpper(lowerLetters)
)
func shuffleLetters(letters []byte) []byte {
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(letters), func(i, j int) {
letters[i], letters[j] = letters[j], letters[i]
})
return letters
}
func randomByte(length int) []byte {
rand.Seed(time.Now().UnixNano())
randomByte := make([]byte, length)
for i := 0; i < length; i++ {
shuffledNumbers := shuffleLetters([]byte(numbers))
shuffledLowerLetters := shuffleLetters([]byte(lowerLetters))
shuffledUpperLetters := shuffleLetters([]byte(upperLetters))
combined = shuffleLetters(
append(
append(
shuffledNumbers,
shuffledLowerLetters...,
),
shuffledUpperLetters...),
)
randomByte[i] = combined[rand.Intn(len(combined))]
}
return randomByte
}
func RandomString(length int) string {
if length == 0 {
length = 8
}
return string(randomByte(length))
}