Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

security: replace math/rand with crypto/rand to generate secret strings (#898)

Jean-Baptiste Bédrunea6e68cc706e9
diff --git a/app/pkg/rand/random.go b/app/pkg/rand/random.go
index 89beda03f..42c6e0267 100644
--- a/app/pkg/rand/random.go
+++ b/app/pkg/rand/random.go
@@ -1,14 +1,10 @@
package rand
import (
- "math/rand"
- "time"
+ "crypto/rand"
+ "math/big"
)
-func init() {
- rand.Seed(time.Now().UnixNano())
-}
-
var chars = []byte("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
// String returns a random string of given length
@@ -18,8 +14,13 @@ func String(n int) string {
}
bytes := make([]byte, n)
+ charsetLen := big.NewInt(int64(len(chars)))
for i := 0; i < n; i++ {
- bytes[i] = chars[rand.Intn(len(chars))]
+ c, err := rand.Int(rand.Reader, charsetLen)
+ if err != nil {
+ panic(err)
+ }
+ bytes[i] = chars[c.Int64()]
}
return string(bytes)