random

package
v0.0.0-...-eb2fa4d Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Cryptographically secure pseudorandom numbers.

This package uses the Linux kernel's

Index

Constants

This section is empty.

Variables

View Source
var (
	GRND_NONBLOCK = 0x1
	GRND_RANDOM   = 0x2
	GRND_INSECURE = 0x4
)

Flags for Getrandom()

View Source
var (
	ErrInvalidSize = errors.New("invalid size")
)

Functions

func Bytes

func Bytes(n int) []byte

Bytes returns n number cryptographically secure random bytes from /dev/random. This function panics if size <= 0 or failed to get random bytes.

func Getrandom

func Getrandom(buf []byte, flags int) error

Getrandom obtains a series of random bytes by filling buffer buf.

By default Getrandom() blocks if no random bytes are available. If the GRND_NONBLOCK flag is set, then Getrandom() does not block in these cases, but instead immediately returns unix.EAGAIN.

If GRND_RANDOM bit is set, then random bytes are drawn from the random source (i.e., the same source as the /dev/random device) instead of the urandom source.

The GRND_INSECURE bit is removes the blocking and allow returning potentially "insecure" random bytes.

See more: man random(4) and random(7)

func Int

func Int() int

Int returns a random int. It panics if failed to get random bytes.

func Intn

func Intn(n int) int

Intn return a random int in the half-open interval [0,n). It panics if n <= 0 or failed to get random bytes.

func String

func String(chars []byte, l int) string

String generates cryptographically **NOT** secure a random string from chars with length l. If chars nil/empty or l < 1, returns an empty string ("").

Types

This section is empty.

Jump to

Keyboard shortcuts

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