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 Rand 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 Rand 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 Rand 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 Rand instance for performance.
Types ¶
type Rand ¶
type Rand interface {
// Bytes 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.
Bytes(size int) ([]byte, error)
// String 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.
String(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)
}
Rand defines the interface for generating cryptographically secure random data. Implementations of this interface are designed for high performance and statistical randomness.
func New ¶ added in v1.3.0
New 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 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.
Decrypted: use New instead.
func NewWithCharset ¶ added in v1.3.0
NewWithCharset creates a new random data generator with a custom character set.