Skip to content

Commit ceafb52

Browse files
committed
refactor: use crypto/rand for cryptographically secure secret generation.
1 parent 13fe2fc commit ceafb52

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

installer/utils/secret.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package utils
22

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

78
func GenerateSecret(size int) string {
89
var characters = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
10+
if size <= 0 {
11+
return ""
12+
}
913

10-
var s string
11-
for {
12-
if len(s) >= size {
13-
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
1419
}
15-
16-
s += string(characters[rand.Intn(len(characters))])
20+
result[i] = characters[num.Int64()]
1721
}
18-
19-
return s
22+
return string(result)
2023
}

0 commit comments

Comments
 (0)