github.com/puellanivis/breton@v0.2.16/lib/rand/random.go (about)

     1  // Package rand provides a wrapper around math/rand that uses crypto/rand for generating seeds.
     2  package rand
     3  
     4  import (
     5  	crand "crypto/rand"
     6  	"time"
     7  )
     8  
     9  var globalRand = New(NewSource(genSeed()))
    10  
    11  func genSeed() int64 {
    12  	// it doesn't matter that this will always read in little-endian mode.
    13  
    14  	b := make([]byte, 8)
    15  	if _, err := crand.Read(b); err != nil {
    16  		panic(err)
    17  	}
    18  
    19  	u := int64(b[0] &^ 0x80) // clear sign bit to ensure no overflows
    20  	for _, b := range b[1:] {
    21  		u <<= 8
    22  		u |= int64(b)
    23  	}
    24  
    25  	return u
    26  }
    27  
    28  // SecureSeed seeds the default global random source with a cryptographically secure seed.
    29  func SecureSeed() {
    30  	globalRand.SecureSeed()
    31  }
    32  
    33  // Reseeder sets up a Reseeder on the default global random source to periodically reseed with a cryptographically secure seed.
    34  func Reseeder(d time.Duration) {
    35  	globalRand.Reseeder(d)
    36  }