Skip to content

Commit f4c6ec0

Browse files
mirskifapranav.goyal
authored andcommitted
Use crypto/rand for password generation
1 parent 01f2946 commit f4c6ec0

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

internal/tls/tls.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ package tls
1717

1818
import (
1919
"bufio"
20+
"crypto/rand"
2021
"fmt"
21-
pwr "math/rand"
2222
"os"
2323
"path/filepath"
2424
"strings"
25-
"time"
2625

2726
"crypto/sha512"
2827
"crypto/x509"
@@ -596,13 +595,13 @@ func addCertificatesToCMSKeystore(cmsKeystore *KeyStoreData) error {
596595

597596
// generateRandomPassword generates a random 12 character password from the characters a-z, A-Z, 0-9
598597
func generateRandomPassword() *sensitive.Sensitive {
599-
pwr.Seed(time.Now().Unix())
600598
validChars := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
601599
validcharArray := []byte(validChars)
602-
password := []byte{}
600+
password := make([]byte, 12)
601+
_, _ = rand.Read(password) // Errors are never returned from crypto/rand.Read()
602+
603603
for i := 0; i < 12; i++ {
604-
// #nosec G404 - this is only for internal keystore and using math/rand pose no harm.
605-
password = append(password, validcharArray[pwr.Intn(len(validcharArray))])
604+
password[i] = validcharArray[int(password[i])%len(validcharArray)]
606605
}
607606

608607
return sensitive.New(password)

0 commit comments

Comments
 (0)