From e96fded8ff12cc940719d49f51e72a9cf461ebbf Mon Sep 17 00:00:00 2001 From: Maximilian Date: Sun, 29 Jan 2023 23:30:21 -0600 Subject: [PATCH] Cryptographically generate password characters --- main.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 8ccd196..78a970a 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( cryptorand "crypto/rand" "encoding/binary" "fmt" + "math/big" mathrand "math/rand" "os" "strconv" @@ -58,9 +59,15 @@ func main() { mathrand.Seed(int64(binary.LittleEndian.Uint64(b[:]))) // Assign every slot of pass to a random allowedCharacter + for i := range pass { // Generate a random int greater than 0 and not to exceed the length of allowedCharacters - pass[i] = allowedCharacters[mathrand.Intn(len(allowedCharacters))] + index, err := cryptorand.Int(cryptorand.Reader, big.NewInt(int64(len(allowedCharacters)))) + if err != nil { + println("Error securely generating random character!") + return + } + pass[i] = allowedCharacters[index.Int64()] } // Print the password