Documentation
¶
Overview ¶
Package rand provides cryptographically secure, high-performance random string and byte generation.
Index ¶
Constants ¶
const ( Digits = "0123456789" Lowercase = "abcdefghijklmnopqrstuvwxyz" Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" Symbols = "!@#$%^&*-_=+" )
const KindAllWithSymbols = KindAlphanumeric | KindSymbol
KindAllWithSymbols represents a combination of digits, lowercase, uppercase letters, and symbols.
const KindAlphanumeric = KindDigit | KindLowerCase | KindUpperCase
KindAlphanumeric represents a combination of digits, lowercase, and uppercase letters.
Variables ¶
This section is empty.
Functions ¶
func RandomBytes ¶ added in v0.3.18
RandomBytes generates n cryptographically secure random bytes using the default alphanumeric character set. This function reuses a single, lazily initialized Generator instance for performance.
func RandomBytesWithSymbols ¶ added in v1.2.0
RandomBytesWithSymbols generates n cryptographically secure random bytes using the alphanumeric and symbol character set. This function reuses a single, lazily initialized Generator instance for performance.
func RandomString ¶ added in v0.3.18
RandomString generates a cryptographically secure random string of length n using the default alphanumeric character set. This function reuses a single, lazily initialized Generator instance for performance.
func RandomStringWithSymbols ¶ added in v1.2.0
RandomStringWithSymbols generates a cryptographically secure random string of length n using the alphanumeric and symbol character set. This function reuses a single, lazily initialized Generator instance for performance.
Types ¶
type Generator ¶ added in v0.3.18
type Generator interface {
// RandBytes generates a new random byte slice of the specified size.
// This method allocates a new slice for each call. For maximum performance when
// repeatedly generating random bytes into a pre-existing buffer, prefer the Read method.
RandBytes(size int) ([]byte, error)
// RandString generates a new random string of the specified size.
// This method involves internal byte slice allocation and conversion to string.
// For scenarios requiring extreme performance and byte-level control, consider
// using RandBytes or Read and managing string conversion manually if profiling
// indicates this is a bottleneck.
RandString(size int) (string, error)
// Read populates the given byte slice with random bytes from the generator's character set.
// It implements the io.Reader interface. This is the most performant method for
// filling pre-allocated buffers, as it avoids internal allocations of the output slice.
Read(p []byte) (n int, err error)
}
Generator defines the interface for generating cryptographically secure random data. Implementations of this interface are designed for high performance and statistical randomness.
func NewGenerator ¶ added in v1.2.0
NewGenerator creates a new random data generator for the given kind of character set. It uses a high-performance array lookup instead of a map.
func NewGeneratorWithCharset ¶ added in v1.2.0
NewGeneratorWithCharset creates a new random data generator with a custom character set.