Update to securely seed math/rand V1.1

This commit is contained in:
Max 2022-10-09 15:57:01 -05:00
parent 388d56b697
commit 03b6ac8375

15
main.go
View File

@ -1,13 +1,15 @@
// GoPass // GoPass
// Author: Maximilian Patterson // Author: Maximilian Patterson
// Version: 1.1
package main package main
import ( import (
cryptorand "crypto/rand"
"encoding/binary"
"fmt" "fmt"
"math/rand" mathrand "math/rand"
"os" "os"
"strconv" "strconv"
"time"
) )
var runes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()_+[]\\{}|;':,./<>?") var runes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()_+[]\\{}|;':,./<>?")
@ -26,11 +28,16 @@ func main() {
pass := make([]rune, size) pass := make([]rune, size)
// Seed rand with time // 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) // Assign every slot of pass to a random rune (generate rand int of length runes to select)
for i := range pass { for i := range pass {
pass[i] = runes[rand.Intn(len(runes))] pass[i] = runes[mathrand.Intn(len(runes))]
} }
// Print the pass :D // Print the pass :D