Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.

Commit 635ead6

Browse files
Use crypto/rand to generate random seed (#223)
1 parent 8576295 commit 635ead6

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

tracer/util.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
package tracer
22

33
import (
4+
cryptorand "crypto/rand"
5+
"encoding/binary"
46
"math/rand"
57
"sync"
68
"time"
79
)
810

911
var (
10-
seededIDGen = rand.New(rand.NewSource(time.Now().UnixNano()))
12+
seededIDGen = rand.New(rand.NewSource(generateSeed()))
1113
// The golang rand generators are *not* intrinsically thread-safe.
1214
seededIDLock sync.Mutex
1315
)
1416

17+
func generateSeed() int64 {
18+
var b [8]byte
19+
_, err := cryptorand.Read(b[:])
20+
if err != nil {
21+
// Cannot seed math/rand package with cryptographically secure random number generator
22+
// Fallback to time.Now()
23+
return time.Now().UnixNano()
24+
}
25+
26+
return int64(binary.LittleEndian.Uint64(b[:]))
27+
}
28+
1529
func randomID() uint64 {
1630
seededIDLock.Lock()
1731
defer seededIDLock.Unlock()

0 commit comments

Comments
 (0)