randbytes

package
v0.12.2 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package randbytes provides interfaces and functions to generate random bytes.

This package is based on the standard library math/rand/v2, which is pseudorandom but reproducible. This package is for scenarios that require not only random data but also the same data next time, such as data for testing. Without the need for reproduction, use crypto/rand.Read instead.

For better performance, all functions in this package are unsafe for concurrency unless otherwise specified.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(src rand.Source, p []byte, n int) []byte

Append generates n random bytes using the specified random value source, appends them to p, and returns the extended byte slice.

The random value source should not be used by others concurrently.

Append panics if the random value source is nil or n is negative.

func Fill

func Fill(src rand.Source, p []byte)

Fill generates random bytes using the specified random value source and writes them into p.

The random value source should not be used by others concurrently.

Fill panics if the random value source is nil.

func Make

func Make(src rand.Source, length int) []byte

Make allocates a []byte with the specified length and fills it with random bytes generated from the specified random value source.

The random value source should not be used by others concurrently.

Make panics if the random value source is nil or the length is negative.

func MakeCapacity

func MakeCapacity(src rand.Source, length int, capacity int) []byte

MakeCapacity is like Make but specifies a different capacity.

The random value source should not be used by others concurrently.

MakeCapacity panics if the random value source is nil, the length is negative, or the capacity is less than the length.

func WriteN

func WriteN(src rand.Source, w io.ByteWriter, n int) (written int, err error)

WriteN generates n random bytes using the specified random value source and writes them into the specified byte writer.

The random value source should not be used by others concurrently.

WriteN returns the number of bytes written and any write error encountered.

It panics if the random value source or the byte writer is nil or n is negative.

Types

type Reader

type Reader interface {
	io.Reader
	io.ByteReader

	// Source returns the random value source used by this reader.
	Source() rand.Source
}

Reader is a pseudorandom byte generator.

It combines io.Reader and io.ByteReader. Its method Read always returns the buffer length (i.e., len(p)) and a nil error. Its method ReadByte always returns a nil error as well.

It is based on the standard library math/rand/v2. During the call to its read methods, its random value source should not be used by others concurrently.

func NewReader

func NewReader(src rand.Source) Reader

NewReader creates a new pseudorandom byte generator with the specified random value source.

During the call to the read methods of the returned Reader, the random value source should not be used by others concurrently.

NewReader panics if the random value source is nil.

Jump to

Keyboard shortcuts

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