We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 13fe2fc commit ceafb52Copy full SHA for ceafb52
1 file changed
installer/utils/secret.go
@@ -1,20 +1,23 @@
1
package utils
2
3
import (
4
- "math/rand"
+ "crypto/rand"
5
+ "math/big"
6
)
7
8
func GenerateSecret(size int) string {
9
var characters = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
10
+ if size <= 0 {
11
+ return ""
12
+ }
13
- var s string
- for {
- if len(s) >= size {
- break
14
+ result := make([]rune, size)
15
+ for i := range result {
16
+ num, err := rand.Int(rand.Reader, big.NewInt(int64(len(characters))))
17
+ if err != nil {
18
+ panic(err) // Consider returning error if signature allows, but keeping panic for now as panic on CSPRNG fail is reasonable for secret gen
19
}
-
- s += string(characters[rand.Intn(len(characters))])
20
+ result[i] = characters[num.Int64()]
21
- return s
22
+ return string(result)
23
0 commit comments