random

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2025 License: MIT Imports: 5 Imported by: 11

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RandomString added in v1.0.0

func RandomString(length int, charset Charset, secure bool) (string, error)

RandomString returns a random string of the given length using the provided charset. If secure is true, it uses a cryptographically secure random generator (returns error on failure). If secure is false, it uses a fast, non-cryptographically secure generator (never returns error). If charset is empty, Alphanumeric is used.

func String

func String(length int) string

String returns a random string of the given length using a fast, non-cryptographically secure generator. For security-sensitive use cases, use StringWithCharset instead.

func StringWithCharset

func StringWithCharset(byteLen int, charset Charset) (string, error)

StringWithCharset returns a cryptographically secure random string of the given byte length, using the provided charset. The randomness is suitable for security-sensitive use cases. Returns an error if the system's secure random number generator fails.

Types

type Charset

type Charset string

Charset represents a set of characters to use for random string generation.

const (
	// Alphanumeric contains all alphabetic and numeric characters (A-Z, a-z, 0-9).
	Alphanumeric Charset = Alphabetic + Numeric
	// Alphabetic contains all uppercase and lowercase English letters (A-Z, a-z).
	Alphabetic Charset = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	// Numeric contains all decimal digits (0-9).
	Numeric Charset = "0123456789"
	// Hex contains all hexadecimal digits (0-9, a-f).
	Hex Charset = Numeric + "abcdef"
)

Jump to

Keyboard shortcuts

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