-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathranstring.go
44 lines (35 loc) · 1.03 KB
/
ranstring.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
package utils
import (
"math/rand"
"time"
)
const charset = "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
const charsetUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
const charsetLower = "abcdefghijklmnopqrstuvwxyz0123456789"
const charsetHexLower = "0123456789abcdef"
const charsetHexUpper = "0123456789ABCDEF"
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))
func RandStringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset)-1)]
}
return string(b)
}
func RandString(length int) string {
return RandStringWithCharset(length, charset)
}
func RandStringUpper(length int) string {
return RandStringWithCharset(length, charsetUpper)
}
func RandStringLower(length int) string {
return RandStringWithCharset(length, charsetLower)
}
func RandomHex(n int) string {
return RandStringWithCharset(n, charsetHexLower)
}
func RandomHexUpper(n int) string {
return RandStringWithCharset(n, charsetHexUpper)
}