From 03b6ac83750fbd0e716614d0c11bade01e424738 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 9 Oct 2022 15:57:01 -0500 Subject: [PATCH] Update to securely seed math/rand V1.1 --- main.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index e4b5dc1..be6ad70 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,15 @@ // GoPass // Author: Maximilian Patterson +// Version: 1.1 package main import ( + cryptorand "crypto/rand" + "encoding/binary" "fmt" - "math/rand" + mathrand "math/rand" "os" "strconv" - "time" ) var runes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()_+[]\\{}|;':,./<>?") @@ -26,11 +28,16 @@ func main() { pass := make([]rune, size) // Seed rand with time - rand.Seed(time.Now().UTC().UnixNano()) + var b [8]byte + _, err = cryptorand.Read(b[:]) + if err != nil { + panic("Error securely seeding rand!") + } + mathrand.Seed(int64(binary.LittleEndian.Uint64(b[:]))) // Assign every slot of pass to a random rune (generate rand int of length runes to select) for i := range pass { - pass[i] = runes[rand.Intn(len(runes))] + pass[i] = runes[mathrand.Intn(len(runes))] } // Print the pass :D