Skip to content

Commit 75c77df

Browse files
committed
use crypto/rand instead of math/rand
1 parent 6e0a06d commit 75c77df

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

pkg/controller/model/utils.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package model
22

33
import (
4-
"math/rand"
4+
"crypto/rand"
5+
"encoding/base64"
56
)
67

7-
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
8-
9-
func RandStringRunes(n int) string {
10-
b := make([]rune, n)
11-
for i := range b {
12-
b[i] = letterRunes[rand.Intn(len(letterRunes))]
8+
func generateRandomBytes(n int) []byte {
9+
b := make([]byte, n)
10+
_, err := rand.Read(b)
11+
if err != nil {
12+
panic(err)
1313
}
14-
return string(b)
14+
return b
15+
}
16+
17+
func RandStringRunes(s int) string {
18+
b := generateRandomBytes(s)
19+
return base64.URLEncoding.EncodeToString(b)
1520
}
1621

1722
func MergeAnnotations(requested map[string]string, existing map[string]string) map[string]string {

0 commit comments

Comments
 (0)