Skip to content

Seeded pseudorandom Reader and rand.Source using the XChaCha20 stream cipher

License

Notifications You must be signed in to change notification settings

ethanent/gopherng

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gopherng

Seeded pseudorandom Reader and rand.Source

Go Reference

Note

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.

Read random bytes from PseudorandomReader

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 ... ]

Generate random values with PseudorandomSource

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

About

Seeded pseudorandom Reader and rand.Source using the XChaCha20 stream cipher

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages