Documentation ¶
Overview ¶
Package rand implements a cryptographically secure pseudorandom number generator.
Index ¶
Examples ¶
Constants ¶
const ( // SeedSize is the required length of seed passed // to New. SeedSize = chacha.KeySize )
Variables ¶
var ( // Reader is a global, shared instance of a // cryptographically strong pseudo-random // generator. // // The seed is read from crypto/rand.Reader. Reader io.Reader = &seedOnFirstRead{} )
Functions ¶
func New ¶
New returns a new pseudorandom generator with the given seed. If seed == nil, the generator seeds itself by reading from crypto/rand.Reader. seed must be SeedSize bytes long.
The Read method on the returned reader always returns the full amount asked for, or else it returns an error.
The generator uses ChaCha20 reseeding after every 1 GB of generated data.
The generator is deterministic for a given seed.
func Read ¶
Read is a helper function that calls Reader.Read using io.ReadFull. On return, n == len(b) if and only if err == nil.
Example ¶
This example reads 10 cryptographically secure pseudorandom numbers from rand.Reader and writes them to a byte slice.
c := 10 b := make([]byte, c) _, err := Read(b) if err != nil { fmt.Println("error:", err) return } // The slice should now contain random bytes instead of only zeroes. fmt.Println(bytes.Equal(b, make([]byte, c)))
Output: false
Types ¶
This section is empty.