Documentation
¶
Overview ¶
Package rndstr provides functions for generating random strings.
The package offers multiple generation strategies with different security/performance tradeoffs:
SimpleReader: Secure, unbiased generation using any io.Reader source. Recommended for security-sensitive applications with crypto/rand.Reader.
Simple: Fast but uses math/rand (not cryptographically secure). Deprecated for security-sensitive use.
SimpleFastReader: Fast but biased. Deprecated in favor of SimpleReader.
Example usage:
// Secure random string s, err := rndstr.SimpleReader(32, rndstr.Alnum, rand.Reader) // Human-readable code (excludes ambiguous chars) code, err := rndstr.SimpleReader(8, rndstr.Code, rand.Reader)
Index ¶
Constants ¶
const ( // Alpha contains lowercase and uppercase ASCII letters. Alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // Alnum contains lowercase and uppercase ASCII letters plus digits. Alnum = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // Code contains uppercase letters and digits, excluding ambiguous // characters (I, O, Q, 0, 1) for improved human readability. Code = "ABCDEFGHJKLMNPRSTUVWXYZ23456789" // Password contains alphanumeric characters plus common special characters. Password = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&~#([-|_^@)]{}$*,?;.:/!<>" )
Character sets for random string generation.
Variables ¶
var ErrInvalidRange = errors.New("rndstr: range must be greater than zero")
ErrInvalidRange is returned when an invalid range parameter is provided.
Functions ¶
func Simple
deprecated
func SimpleFastReader
deprecated
SimpleFastReader generates a random string using modulo reduction.
Deprecated: This function has biased output when the character set length does not evenly divide 256. For example, with Alnum (62 chars), some characters are ~1.6% more likely than others. Use SimpleReader instead for unbiased output.
Example: rndstr.SimpleFastReader(10, rndstr.Alnum, rand.Reader)
func SimpleReader ¶
SimpleReader generates a random string using the provided random source.
This function uses unbiased random selection and is suitable for security-sensitive applications when used with crypto/rand.Reader. It consumes at least 4 bytes of random input per generated character.
Example: rndstr.SimpleReader(10, rndstr.Alnum, rand.Reader)
Types ¶
This section is empty.