rand

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package rand provides cryptographically secure, high-performance random string and byte generation.

Index

Constants

View Source
const (
	Digits    = "0123456789"
	Lowercase = "abcdefghijklmnopqrstuvwxyz"
	Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	Symbols   = "!@#$%^&*-_=+"
)
View Source
const KindAllWithSymbols = KindAlphanumeric | KindSymbol

KindAllWithSymbols represents a combination of digits, lowercase, uppercase letters, and symbols.

View Source
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

func RandomBytes(n int) ([]byte, error)

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

func RandomBytesWithSymbols(n int) ([]byte, error)

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

func RandomString(n int) (string, error)

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

func RandomStringWithSymbols(n int) (string, error)

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 Kind

type Kind int

Kind defines the character set to be used for random string generation.

const (
	KindDigit     Kind = 1 << iota // KindDigit represents digit characters (0-9).
	KindLowerCase                  // KindLowerCase represents lowercase letters (a-z).
	KindUpperCase                  // KindUpperCase represents uppercase letters (A-Z).
	KindSymbol                     // KindSymbol represents common symbol characters.
)

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

func New(kind Kind) Rand

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

func NewGenerator(kind Kind) Rand

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

func NewWithCharset(charset string) Rand

NewWithCharset creates a new random data generator with a custom character set.

Jump to

Keyboard shortcuts

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