csmrand

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2022 License: MIT Imports: 3 Imported by: 0

README

csmrand

Go Reference Build Status

Golang's math/rand backed onto crypto/rand's CSPRNG.

This package provides an implementation of Go's math/rand API that is backed by the CSPRNG in crypto/rand. This allows using the nicer API from math/rand, but still ensuring that the random number source is cryptographically-secure. See the documentation for more details.

Documentation

Overview

This package provides an implementation of Go's math/rand API that is backed by the CSPRNG in crypto/rand. This allows using the nicer API from math/rand, but still ensuring that the random number source is cryptographically-secure.

If there is a case where this package cannot obtain random data from crypto/rand, it will panic. This is largely the solution you want, since this should only occur (on Linux) when the /dev/urandom pool hasn't been initialized yet. In this case, the calling code can't do anything sensible with the error anyway. Also, panicking on this error allows us to maintain the same API as math/rand.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExpFloat64

func ExpFloat64() float64

func Float32

func Float32() float32

func Float64

func Float64() float64

func Int

func Int() int

func Int31

func Int31() int32

func Int31n

func Int31n(n int32) int32

func Int63

func Int63() int64

func Int63n

func Int63n(n int64) int64

func Intn

func Intn(n int) int

func NormFloat64

func NormFloat64() float64

func Perm

func Perm(n int) []int

func Read

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

func Seed

func Seed(seed int64)

func Shuffle

func Shuffle(n int, swap func(i, j int))

func Uint32

func Uint32() uint32

func Uint64

func Uint64() uint64

Types

type CSRandSource

type CSRandSource struct{}

CSRandSource is a math/rand.Source that wraps crypto/rand.Reader.

func (CSRandSource) Int63

func (r CSRandSource) Int63() int64

Int63 implements the math/rand.Source interface. Will panic if an error occurs when reading the underlying Reader.

func (CSRandSource) Seed

func (r CSRandSource) Seed(s int64)

Seed implements the math/rand.Source interface. Does nothing.

func (CSRandSource) Uint64

func (r CSRandSource) Uint64() uint64

Uint64 implements the math/rand.Source64 interface. Will panic if an error occurs when reading the underlying Reader.

type CSStatefulRandSource added in v0.2.0

type CSStatefulRandSource struct {
	// contains filtered or unexported fields
}

CSStatefulRandSource is a math/rand.Source that wraps crypto/rand.Reader that is not safe for use by concurrent goroutines. Similar to math/rand.Source, protect its usage by using a sync.Pool (recommended) or with a sync.Mutex.

func (*CSStatefulRandSource) Int63 added in v0.2.0

func (r *CSStatefulRandSource) Int63() int64

Int63 implements the math/rand.Source interface. Will panic if an error occurs when reading the underlying Reader.

func (*CSStatefulRandSource) Seed added in v0.2.0

func (r *CSStatefulRandSource) Seed(s int64)

Seed implements the math/rand.Source interface. Does nothing.

func (*CSStatefulRandSource) Uint64 added in v0.2.0

func (r *CSStatefulRandSource) Uint64() uint64

Uint64 implements the math/rand.Source64 interface. Will panic if an error occurs when reading the underlying Reader.

type Rand

type Rand struct{}

A source of random numbers. Mimics the type of the same name in math/rand. This is useful if you want to use this package as a drop-in replacement for code that uses math/rand.

Note that since this package obtains all state from the crypto/rand.Reader, all methods of this struct use a single shared implementation.

func (Rand) ExpFloat64

func (r Rand) ExpFloat64() float64

func (Rand) Float32

func (r Rand) Float32() float32

func (Rand) Float64

func (r Rand) Float64() float64

func (Rand) Int

func (r Rand) Int() int

func (Rand) Int31

func (r Rand) Int31() int32

func (Rand) Int31n

func (r Rand) Int31n(n int32) int32

func (Rand) Int63

func (r Rand) Int63() int64

func (Rand) Int63n

func (r Rand) Int63n(n int64) int64

func (Rand) Intn

func (r Rand) Intn(n int) int

func (Rand) NormFloat64

func (r Rand) NormFloat64() float64

func (Rand) Perm

func (r Rand) Perm(n int) []int

func (Rand) Read

func (r Rand) Read(p []byte) (n int, err error)

func (Rand) Seed

func (r Rand) Seed(seed int64)

Note: does nothing.

func (Rand) Shuffle

func (r Rand) Shuffle(n int, swap func(i, j int))

func (Rand) Uint32

func (r Rand) Uint32() uint32

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL