-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.go
56 lines (45 loc) · 1.22 KB
/
random.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
package security
import (
"crypto/rand"
"encoding/base64"
"fmt"
)
// GenerateRandomBytes returns securely generated random bytes
func GenerateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
// Note that err == nil only if we read len(b) bytes.
if err != nil {
return nil, err
}
return b, nil
}
// GenerateRandomString returns a URL-safe, base64 encoded securely generated random string.
func GenerateRandomString(n int) (string, error) {
b, err := GenerateRandomBytes(n)
return base64.URLEncoding.EncodeToString(b), err
}
// GenerateRandomCode returns a securely generated random string that consists of numbers that has length of 2*n
func GenerateRandomCode(n int) (string, error) {
b, err := GenerateRandomBytes(n)
if err != nil {
return "", err
}
code := ""
for i := 0; i < n; i++ {
code = code + fmt.Sprintf("%02d", b[i]%100)
}
return code, err
}
// GenerateRandomStringByPool returns a securely generated random string based on pool.
func GenerateRandomStringByPool(n int, pool string) (string, error) {
l := byte(len(pool))
b, err := GenerateRandomBytes(n)
if err != nil {
return "", err
}
for i := 0; i < n; i++ {
b[i] = pool[(b[i])%l]
}
return string(b), nil
}