Seeded pseudorandom Reader and rand.Source
This package was implemented for the purposes of the CubeDance game and may not be applicable for your purposes. It has not been independently audited for security.
This package includes a pseudorandom Reader. It initializes, based on a provided seed, an XChaCha20 stream cipher which is used to encrypt 0s in order to generate random bytes.
Internally the seed is hashed to produce a key and nonce to initialize the XChaCha20 cipher. This is not a substitute for using a sufficiently high-entropy seed or sufficiently stretching a lower-entropy seed.
// Create PseudorandomReader with a high-entropy seed.
r := gopherng.NewReader([]byte{1, 2, 3, /* ... use a high-entropy seed */})
p := make([]byte, 256)
_, err := r.Read(p)
if err != nil {
/* ... */
}
fmt.Println(p)
// => [43 98 21 80 122 33 48 91 135 248 242 ... ]
This package also includes a pseudorandom rand.Source
which can be used to
initialize a rand.Rand
(from math/rand
) like so:
// Create PseudorandomSource with a high-entropy seed.
s := gopherng.NewSource([]byte{1, 2, 3, /* ... use a high-entropy seed */})
// Create a rand.Rand from the Source.
r := rand.New(s)
fmt.Println(r.Float64())
// => 0.7124063346129034