...

Package rand

import "crypto/rand"
Overview
Index
Examples

Overview ▾

Package rand implements a cryptographically secure random number generator.

Variables

Reader is a global, shared instance of a cryptographically secure random number generator. It is safe for concurrent use.

In FIPS 140-3 mode, the output passes through an SP 800-90A Rev. 1 Deterministric Random Bit Generator (DRBG).

var Reader io.Reader

func Int

func Int(rand io.Reader, max *big.Int) (n *big.Int, err error)

Int returns a uniform random value in [0, max). It panics if max <= 0, and returns an error if rand.Read returns one.

func Prime

func Prime(rand io.Reader, bits int) (*big.Int, error)

Prime returns a number of the given bit length that is prime with high probability. Prime will return error for any error returned by rand.Read or if bits < 2.

func Read

func Read(b []byte) (n int, err error)

Read fills b with cryptographically secure random bytes. It never returns an error, and always fills b entirely.

Read calls io.ReadFull on Reader and crashes the program irrecoverably if an error is returned. The default Reader uses operating system APIs that are documented to never return an error on all but legacy Linux systems.

Example

Code:

// Note that no error handling is necessary, as Read always succeeds.
key := make([]byte, 32)
rand.Read(key)

func Text 1.24

func Text() string

Text returns a cryptographically random string using the standard RFC 4648 base32 alphabet for use when a secret string, token, password, or other text is needed. The result contains at least 128 bits of randomness, enough to prevent brute force guessing attacks and to make the likelihood of collisions vanishingly small. A future version may return longer texts as needed to maintain those properties.