-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (52 loc) · 1.33 KB
/
main.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
package random
import (
"io"
"math/rand"
)
// NewRandomDataReader creates an *io.ReadCloser returning
// random data of the specified length.
func NewRandomDataReader(length int) *RandomDataReader {
rdr := &RandomDataReader{
Length: length,
randomChars: "ABC",
}
return rdr
}
type RandomDataReader struct {
Length int
count int
randomChars string
}
func (r *RandomDataReader) Read(b []byte) (int, error) {
c := 0
for i := 0; i < len(b) && r.count < r.Length; i++ {
b[i] = r.randomChars[i%len(r.randomChars)]
c++
r.count++
}
if c == 0 {
return 0, io.EOF
}
return c, nil
}
func (r *RandomDataReader) Close() error {
return nil
}
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var moreLetterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_.,:;")
// GenerateRandomString generates a random string of the length provided as input.
func GenerateRandomString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
// GenerateStrongerRandomString generates a random string of the length provided as input.
func GenerateStrongerRandomString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = moreLetterRunes[rand.Intn(len(moreLetterRunes))]
}
return string(b)
}