From fe7ef0d0b1e445615af86672e0ee13835fcb792c Mon Sep 17 00:00:00 2001 From: Pablo Deymonnaz Date: Tue, 24 Sep 2024 17:35:36 -0300 Subject: [PATCH 1/2] Change rand library for a a cryptographically secure random number generator one --- cmd/egnkey/generate/generate.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/egnkey/generate/generate.go b/cmd/egnkey/generate/generate.go index 28691e80..56cc5c9a 100644 --- a/cmd/egnkey/generate/generate.go +++ b/cmd/egnkey/generate/generate.go @@ -1,9 +1,9 @@ package generate import ( + "crypto/rand" "encoding/hex" "fmt" - "math/rand" "os" "path/filepath" "time" From a04c60d168a674bd66b409e38af6b09b408042cb Mon Sep 17 00:00:00 2001 From: Pablo Deymonnaz Date: Tue, 24 Sep 2024 18:01:51 -0300 Subject: [PATCH 2/2] Fixing usage of crypto/rand in generateRandomPassword --- cmd/egnkey/generate/generate.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/cmd/egnkey/generate/generate.go b/cmd/egnkey/generate/generate.go index 56cc5c9a..e7350144 100644 --- a/cmd/egnkey/generate/generate.go +++ b/cmd/egnkey/generate/generate.go @@ -4,9 +4,9 @@ import ( "crypto/rand" "encoding/hex" "fmt" + "math/big" "os" "path/filepath" - "time" "github.com/Layr-Labs/eigensdk-go/crypto/bls" @@ -167,7 +167,7 @@ func generateBlsKeys(numKeys int, path string, passwordFile, privateKeyFile *os. return err } - password := generateRandomPassword() + password, err := generateRandomPassword() if err != nil { return err } @@ -214,7 +214,7 @@ func generateECDSAKeys(numKeys int, path string, passwordFile, privateKeyFile *o return err } - password := generateRandomPassword() + password, err := generateRandomPassword() if err != nil { return err } @@ -242,10 +242,7 @@ func generateECDSAKeys(numKeys int, path string, passwordFile, privateKeyFile *o return nil } -func generateRandomPassword() string { - // Seed the random number generator - random := rand.New(rand.NewSource(time.Now().UnixNano())) - +func generateRandomPassword() (string, error) { // Define character sets for the password uppercaseLetters := "ABCDEFGHIJKLMNOPQRSTUVWXYZ" lowercaseLetters := "abcdefghijklmnopqrstuvwxyz" @@ -262,7 +259,11 @@ func generateRandomPassword() string { // Generate the password password := make([]byte, passwordLength) for i := range password { - password[i] = allCharacters[random.Intn(len(allCharacters))] + v, err := rand.Int(rand.Reader, big.NewInt(int64(len(allCharacters)))) + if err != nil { + return "", err // Return empty string in case of error + } + password[i] = allCharacters[v.Int64()] } - return string(password) + return string(password), nil }