Skip to content

Commit 8807567

Browse files
mirskifapranav.goyal
authored andcommitted
Use crypto/rand for password generation
1 parent 98d2c2a commit 8807567

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
@@ -18,13 +18,12 @@ package tls
1818
import (
1919
"bufio"
2020
"crypto"
21+
"crypto/rand"
2122
"fmt"
22-
pwr "math/rand"
2323
"os"
2424
"path"
2525
"path/filepath"
2626
"strings"
27-
"time"
2827

2928
"crypto/sha512"
3029
"crypto/x509"
@@ -677,13 +676,13 @@ func addCertificatesToCMSKeystore(cmsKeystore *KeyStoreData) error {
677676

678677
// generateRandomPassword generates a random 12 character password from the characters a-z, A-Z, 0-9
679678
func generateRandomPassword() *sensitive.Sensitive {
680-
pwr.Seed(time.Now().Unix())
681679
validChars := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
682680
validcharArray := []byte(validChars)
683-
password := []byte{}
681+
password := make([]byte, 12)
682+
_, _ = rand.Read(password) // Errors are never returned from crypto/rand.Read()
683+
684684
for i := 0; i < 12; i++ {
685-
// #nosec G404 - this is only for internal keystore and using math/rand pose no harm.
686-
password = append(password, validcharArray[pwr.Intn(len(validcharArray))])
685+
password[i] = validcharArray[int(password[i])%len(validcharArray)]
687686
}
688687

689688
return sensitive.New(password)

0 commit comments

Comments
 (0)