From cf5407f2a93980785e60be76910e65db03d21aab Mon Sep 17 00:00:00 2001 From: Kwinten Van den Berghe Date: Wed, 17 May 2023 11:48:30 +0200 Subject: [PATCH] Use cryptographic random as RNG seed for agent ID (#32) * Use seeded random number generator for agent id * Use cryptographic random as RNG seed --- core/configuration/dt_config_provider.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/core/configuration/dt_config_provider.go b/core/configuration/dt_config_provider.go index 4bfb66c..ad89254 100644 --- a/core/configuration/dt_config_provider.go +++ b/core/configuration/dt_config_provider.go @@ -15,6 +15,8 @@ package configuration import ( + cryptoRand "crypto/rand" + "encoding/binary" "errors" "fmt" "math/rand" @@ -171,6 +173,15 @@ func validateConfiguration(config *DtConfiguration) error { } func generateAgentId() int64 { - rng := rand.New(rand.NewSource(time.Now().UnixNano())) + var rng *rand.Rand + + var seed [8]byte + _, err := cryptoRand.Read(seed[:]) + if err != nil { + rng = rand.New(rand.NewSource(time.Now().UnixNano())) + } else { + rng = rand.New(rand.NewSource(int64(binary.LittleEndian.Uint64(seed[:])))) + } + return int64(rng.Uint64()) }